Reputation: 1740
I'm new to Swift, and I was recently learning about UITextField
and how it delegates. I was wondering if there was a simple way to dismiss the keyboard when taping outside of it (somewhere else in the view). Currently, I am using UITapGestureRecognizer
to do this. It works, but I was wondering if there was a simpler way to do it.
Upvotes: 1
Views: 1290
Reputation: 2444
Set the delegate of UITextField
, Like as UITableviewDataSource,Delegate
. Write yourTextFieldDelegate.delegate = self
in viewDidLoad()
Then, write this function:
func textFieldShouldReturn(textField: UITextField) -> Bool {
titleTextField.resignFirstResponder()
return true
}
Upvotes: 0
Reputation: 6892
You can use this method to dismiss the keyboard by tapping anywhere on the screen
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
But be sure to set the delegates beforehand.
Upvotes: 1