我想让用户在UIDatePicker
中设置时间,该时间在单击UITextField
之后显示,但是带有完成按钮的工具栏仅在用户单击UITextField
两次时显示。
func showDatePicker(){
//Formate Date
datepickerView.datePickerMode = .time
//ToolBar
let toolbar = UIToolbar();
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker));
toolbar.setItems([doneButton], animated: false)
workHoursStartTextField.inputAccessoryView = toolbar
}
@objc func doneButtonDatePicker(){
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
workHoursStartTextField.text = formatter.string(from: datepickerView.date)
self.view.endEditing(true)
}
@objc func tapOnStartWork(gestureReconizer: UITapGestureRecognizer) {
print("Start work")
datepickerView.isHidden = false
showDatePicker()
}
和调用方法
override func viewDidLoad() {
super.viewDidLoad()
let tapStartWork = UITapGestureRecognizer(target: self, action: #selector(tapOnStartWork(gestureReconizer:)))
workHoursStartTextField.addGestureRecognizer(tapStartWork)
}
当我单击UITextField
文本时,会在控制台中打印“开始工作”文本,但工具栏按钮仍处于隐藏状态。