Skip to content

Instantly share code, notes, and snippets.

@reyandrey
Created July 7, 2019 21:16
Show Gist options
  • Save reyandrey/b84bfaf7efcf78a182714eb92a739dff to your computer and use it in GitHub Desktop.
Save reyandrey/b84bfaf7efcf78a182714eb92a739dff to your computer and use it in GitHub Desktop.
Hide keyboard when tapped around
// Put this piece of code anywhere you like
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment