1
votes

I have a text field and a date picker. I want to enter text into the uitextview but once the user interacts with the datepicker I want to hide the keyboard. I've done 3 things:

1) Add a tap gesture recognizer to the rootcontroller's view object which calls a method running self.view.endEditing(true). This hides the keyboard ONLY when the user interacts with anything outside of the textview and datepicker.

2) I've tried creating an IBAction for touch down and then running self.view.endEditing(true). This doesn't work.

3) I've also tried adding a tap gesture recognizer to the date picker but it doesn't get called.

Any suggestions? Thanks.

1

1 Answers

2
votes

You can try

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        if self.datePicker.frame.contains(touch.location(in: view)) {
          self.view.endEditing(true)
        } 
    }
}