Skip to content

Instantly share code, notes, and snippets.

@tommycarpi
Last active November 28, 2016 14:45
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 tommycarpi/1c4514ba523d0e5ed77f586952998f24 to your computer and use it in GitHub Desktop.
Save tommycarpi/1c4514ba523d0e5ed77f586952998f24 to your computer and use it in GitHub Desktop.
UIViewController extension to dismiss the keyboard while tapping on the background

Dismiss keyboard by tapping on view. Swift 3.

extension UIViewController {
    func dismissKeyboardOnTap() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)
    }
    
    func dismissKeyboard() {
        view.endEditing(true)
    }
    
// Call using:
// self.dismissKeyboardOnTap() in your viewDidLoad() 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment