Best way to dismiss Keyboard in a View Controller iOS (Swift)
First way: Implement UITextFieldDelegate’s textFieldShouldReturn method and dismiss current keyboard.
This solution seems good enough but not all keyboards have return button.
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
text1.delegate = self
text2.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
text1.resignFirstResponder()
return true
}
}
Second way: when textfield has no return keys eg. (UIKeyboardType.numberPad)
class ViewController: UIViewController {
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setupTextFields()
}
func setupTextFields() {
let toolbar = UIToolbar()
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done,
target: self, action: #selector(doneButtonTapped))
toolbar.setItems([flexSpace, doneButton], animated: true)
toolbar.sizeToFit()
text1.inputAccessoryView = toolbar
text2.inputAccessoryView = toolbar
}
@objc func doneButtonTapped() {
view.endEditing(true)
}
}
Third: what could be better than tapping anywhere on the view controller to dismiss the keyboard.
Just one line of code would serve our purpose. Add a tap gesture recogniser on self.view, set target as self.view and set selector as endEditing: (in ViewDidLoad() or anywhere you like)
class ViewController: UIViewController {
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}