Created
March 19, 2021 16:12
-
-
Save artturijalli/a8488aee09d0ec1e531a73eace6c2b18 to your computer and use it in GitHub Desktop.
Extensions for dismissing keyboard in SwiftUI by tapping outside the text field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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