dismiss keyboard on tap swift code example
Example 1: swift dismiss keyboard on return
extension UIViewController: UITextFieldDelegate{
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
}
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
Example 2: how to dismiss keyboard in swift
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
Example 3: dismiss keyboard on tap outside swiftui
extension UIApplication {
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}