Skip to content

Instantly share code, notes, and snippets.

@dfrobison
Created February 8, 2020 21:44
Show Gist options
  • Save dfrobison/4759c9920ef1a583c780bb74154c2279 to your computer and use it in GitHub Desktop.
Save dfrobison/4759c9920ef1a583c780bb74154c2279 to your computer and use it in GitHub Desktop.
[Dismissing the keyboard] One way to dismiss the keyboard given that the tapGesture is not overwritten somewhere else
struct DismissingKeyboard: ViewModifier {
func body(content: Content) -> some View {
content
.onTapGesture {
let keyWindow = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
keyWindow?.endEditing(true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment