1. KeyboardDismisser:- is a little Swift 4.x pod that adds a button over keyboard so that users can dismiss keyboard easily.
Source: https://cocoapods.org/pods/KeyboardDismisser
2. IQKeyboardManager:- good solution
Source: https://cocoapods.org/pods/IQKeyboardManagerSwift
// Source: https://stackoverflow.com/questions/24126678/close-ios-keyboard-by-touching-anywhere-using-swift
// 1.
override func viewDidLoad() {
super.viewDidLoad()
//Looks for single or multiple taps.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
//tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
}
//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
self.view.endEditing(true)
}
// 2. Above website contains easy method of creating an extension of textfield for multiple view controllers
// https://stackoverflow.com/questions/24126678/close-ios-keyboard-by-touching-anywhere-using-swift