Skip to content

Instantly share code, notes, and snippets.

@RealEmmettS
Created February 17, 2022 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealEmmettS/cdc4c84a678ca59359a026ff73881920 to your computer and use it in GitHub Desktop.
Save RealEmmettS/cdc4c84a678ca59359a026ff73881920 to your computer and use it in GitHub Desktop.
//Paste in an 'extra' (global) Swift file, or after the class in a viewController file
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
//Paste in any viewController which presents a keyboard
self.hideKeyboardWhenTappedAround()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment