Created
July 7, 2019 21:16
-
-
Save reyandrey/b84bfaf7efcf78a182714eb92a739dff to your computer and use it in GitHub Desktop.
Hide keyboard when tapped around
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
// 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