I want to implement an action, when if the user tap somewhere on the screen, then the keyboard should disappear.
I have an app, that look as follow
When I tap on textfield then the keyboard appears, that is good.
Now I want, when I tap somewhere on the screen, then the keyboard should disappear.
I implemented a tap gesture recognizer with following property value:
And the tap gesture recognizer I bound with following action in the view controller
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var numberField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func textFieldDoneEdition(sender: UITextField) {
sender.resignFirstResponder()
}
@IBAction func onTapGestureRecognized(sender: AnyObject) {
nameField.resignFirstResponder()
numberField.resignFirstResponder()
}
}
Tap gesture recognizer is bound to the function. And it does not work at all, when I tap somewhere on the screen. The keyboard is still there. What am I doing wrong?