Last active
November 10, 2020 19:35
-
-
Save jbadger3/f8a9a33c2517aae0078d3cef31b1531c to your computer and use it in GitHub Desktop.
iOS manual keyboard dismissal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
@IBOutlet weak var textField: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) | |
self.view.addGestureRecognizer(tapGesture) | |
} | |
@IBAction func buttonPressed(_ sender: Any) { | |
dismissKeyboardFrom(view: textField) | |
} | |
@objc func dismissKeyboardFrom(view: UIView) { | |
view.resignFirstResponder() | |
// or view.endEditing() | |
} | |
@objc func dismissKeyboard() { | |
self.view.endEditing(true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment