仅在双击TextField后,UIDatePicker中的工具栏按钮才会显示

时间:2019-04-19 18:15:33

标签: ios swift uidatepicker

我想让用户在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文本时,会在控制台中打印“开始工作”文本,但工具栏按钮仍处于隐藏状态。

0 个答案:

没有答案