Skip to content

Instantly share code, notes, and snippets.

@artturijalli
Created March 19, 2021 16:12
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 artturijalli/a8488aee09d0ec1e531a73eace6c2b18 to your computer and use it in GitHub Desktop.
Save artturijalli/a8488aee09d0ec1e531a73eace6c2b18 to your computer and use it in GitHub Desktop.
Extensions for dismissing keyboard in SwiftUI by tapping outside the text field
extension UIApplication {
func handleKeyboard() {
guard let window = windows.first else { return }
let tapRecognizer = UITapGestureRecognizer(target: window, action: #selector(UIView.endEditing))
tapRecognizer.cancelsTouchesInView = false
tapRecognizer.delegate = self
window.addGestureRecognizer(tapRecognizer)
}
}
extension UIApplication: UIGestureRecognizerDelegate {
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return false
}
}
@karm435
Copy link

karm435 commented Jan 12, 2024

This creates a bug if there is a segment control on the view and textfield both. Clicking the segment control while keyboard is open only dismiss the keyboard and does not switch the segment control

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment