Skip to content

Instantly share code, notes, and snippets.

@jazzedge
Last active December 19, 2017 22:22
Show Gist options
  • Save jazzedge/3ae00d7322c28559f92801bb552a6511 to your computer and use it in GitHub Desktop.
Save jazzedge/3ae00d7322c28559f92801bb552a6511 to your computer and use it in GitHub Desktop.
See: https://freakycoder.com/ios-notes-31-how-to-hide-keyboard-by-touching-anywhere-cdf4f0c5151c
01. Create an extension
import UIKit
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)
}
}
02. Add this to viewDidLoad
// Hide Keyboard after tap is done
self.hideKeyboardWhenTappedAround()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment