Skip to content

Instantly share code, notes, and snippets.

@khawajafarooq
Last active September 28, 2017 04:31
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 khawajafarooq/fd61b7cde117c3c32a7d2ef7f5578ac6 to your computer and use it in GitHub Desktop.
Save khawajafarooq/fd61b7cde117c3c32a7d2ef7f5578ac6 to your computer and use it in GitHub Desktop.
Hiding keyboard category on clicking anywhere on the screen
import Foundation
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard(_ sender: UITapGestureRecognizer) {
self.view.endEditing(true)
}
}
// How to use it?
...
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment