Skip to content

Instantly share code, notes, and snippets.

@Mez0
Last active June 23, 2020 15:33
Show Gist options
  • Save Mez0/6243f28e168fc894b4c329717e8d16d3 to your computer and use it in GitHub Desktop.
Save Mez0/6243f28e168fc894b4c329717e8d16d3 to your computer and use it in GitHub Desktop.
A UIViewController extension for dismissing keyboard
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment