我有一个UIDatePicker
,我想像在关闭键盘一样在按钮外轻按时将其隐藏,我已经尝试了所有操作,但似乎无济于事。
import UIKit
ViewController类:UIViewController {
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var BDbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
datePicker.isHidden = true
datePicker?.datePickerMode = .date
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
BDbtn.setTitle("Pick a date!", for: .normal)
}
@IBAction func dateChanged(_ sender: Any) {
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
BDbtn.setTitle(formatter.string(from: datePicker.date), for: .normal)
}
@IBAction func BDpressed(_ sender: Any) {
if datePicker.isHidden {
datePicker.isHidden = false
} else {
datePicker.isHidden = true
}
}
}
答案 0 :(得分:0)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
答案 1 :(得分:0)
如果您在Scrollview或主UIView中添加了datePicker。您可以使用UITapGestureRecogniser并在手势方法中添加一行代码:
self.view.endEditing(true)
就是这样。