Skip to content

Instantly share code, notes, and snippets.

@MrLotU
Created August 7, 2020 12:42
Show Gist options
  • Save MrLotU/1c81566b670bd994a6e3e06bbbdb43c6 to your computer and use it in GitHub Desktop.
Save MrLotU/1c81566b670bd994a6e3e06bbbdb43c6 to your computer and use it in GitHub Desktop.
How to hide a keyboard in SwiftUI
extension UIApplication {
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
extension View {
func endEditing() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
extension UIView {
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
UIApplication.shared.windows
.first { $0.isKeyWindow }?
.endEditing(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment