Skip to content

Instantly share code, notes, and snippets.

@jimbrayrcp
Last active May 6, 2020 16:06
Show Gist options
  • Save jimbrayrcp/55c902545a356a5c3c01b084a9431be1 to your computer and use it in GitHub Desktop.
Save jimbrayrcp/55c902545a356a5c3c01b084a9431be1 to your computer and use it in GitHub Desktop.
Works in Swift 5: iOS: 13.1 Dismiss a keyboard when tapping outside of field
// ************************************
// ADD TO VIEW DID LOAD
// ------------------------------------
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIInputViewController.dismissKeyboard))
//Uncomment to prevent interference of cancel button
//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