One possible solution is to detect the click off the keyboard using a GestureRecognizer
. Then call the resignFirstResponder
method on the textfield instance or call endEditing
which forces any descendant of the view to be the first to "give up" this condition.
class ViewController: UIViewController , UITextFieldDelegate {
@IBOutlet var textField : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
var tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
func dismissKeyboard(){
//textField.resignFirstResponder()
view.endEditing(true)
}
}