如何在点击手势上显示uidatePicker日历?
我有一个嵌入在uiimageView中的uidatePicker视图。我想创建一个功能,当uiimageView被点击时,uidatePicker的日历出现在屏幕上。我还希望uidatePicker具有与UIImageView匹配的自定义背景颜色。
我尝试的是:
- 将uitapgestureRerecognizer添加到UIImageView,使UIDEDPICKER成为第一响应者
- ,将UIDESPICKER的背景颜色设置为自定义的RGB颜色(也可以清楚地尝试),
是否有一种使UIDESPICKER在UidatePicker的日程表中出现的方法单击UIimageView并将自定义颜色添加到UIDESPICKER?
class VieController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(section)
section.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
section.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
section.centerXAnchor.constraint(equalTo: view.centerXAnchor),
section.widthAnchor.constraint(equalTo: view.widthAnchor,
multiplier: 0.8)
])
}
lazy var datePicker: UIDatePicker = {
let picker = UIDatePicker()
picker.datePickerMode = .date
picker.translatesAutoresizingMaskIntoConstraints = false
picker.backgroundColor = UIColor(red: 246.0, green: 240.0, blue: 220.0, alpha: 1.0)
return picker
}()
lazy var section: UIImageView = {
let iv = UIImageView(image: Image.setButtonBg)
iv.isUserInteractionEnabled = true
iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapCalendar)))
let calendarIV = UIImageView()
calendarIV.image = UIImage(systemName: "calendar")
calendarIV.translatesAutoresizingMaskIntoConstraints = false
iv.addSubview(datePicker)
iv.addSubview(calendarIV)
NSLayoutConstraint.activate([
datePicker.centerXAnchor.constraint(equalTo: iv.centerXAnchor),
datePicker.centerYAnchor.constraint(equalTo: iv.centerYAnchor),
calendarIV.centerYAnchor.constraint(equalTo: iv.centerYAnchor),
calendarIV.leadingAnchor.constraint(equalTo: iv.leadingAnchor,
constant: 15)
])
return iv
}()
@objc func didTapCalendar() {
datePicker.becomeFirstResponder()
}
}
I have a UIDatePicker view that is embedded in a UIImageView. I want to create a functionality where when the UIImageView is tapped, the UIDatePicker's calendar appears on the screen. I also want the UIDatePicker to have a custom background color that matches the UIImageView.
What I tried:
- Adding UITapGestureRecognizer to the UIImageView to make the UIDatePicker a first responder
- Setting UIDatePicker's backgroundColor to a custom RGB color (tried clear as well)
Is there a way to make the UIDatePicker's calendar appear when the UIImageView is clicked and add a custom color to the UIDatePicker?
class VieController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(section)
section.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
section.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
section.centerXAnchor.constraint(equalTo: view.centerXAnchor),
section.widthAnchor.constraint(equalTo: view.widthAnchor,
multiplier: 0.8)
])
}
lazy var datePicker: UIDatePicker = {
let picker = UIDatePicker()
picker.datePickerMode = .date
picker.translatesAutoresizingMaskIntoConstraints = false
picker.backgroundColor = UIColor(red: 246.0, green: 240.0, blue: 220.0, alpha: 1.0)
return picker
}()
lazy var section: UIImageView = {
let iv = UIImageView(image: Image.setButtonBg)
iv.isUserInteractionEnabled = true
iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapCalendar)))
let calendarIV = UIImageView()
calendarIV.image = UIImage(systemName: "calendar")
calendarIV.translatesAutoresizingMaskIntoConstraints = false
iv.addSubview(datePicker)
iv.addSubview(calendarIV)
NSLayoutConstraint.activate([
datePicker.centerXAnchor.constraint(equalTo: iv.centerXAnchor),
datePicker.centerYAnchor.constraint(equalTo: iv.centerYAnchor),
calendarIV.centerYAnchor.constraint(equalTo: iv.centerYAnchor),
calendarIV.leadingAnchor.constraint(equalTo: iv.leadingAnchor,
constant: 15)
])
return iv
}()
@objc func didTapCalendar() {
datePicker.becomeFirstResponder()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论