Skip to content

Instantly share code, notes, and snippets.

@perlguy99
Created November 27, 2019 16:25
Show Gist options
  • Save perlguy99/517d27fca15e574e12dbee5e97a928b1 to your computer and use it in GitHub Desktop.
Save perlguy99/517d27fca15e574e12dbee5e97a928b1 to your computer and use it in GitHub Desktop.
Dismissing Keyboard View Modifier
//
// DismissingKeyboard+ViewModifier.swift
//
// Created by Michalski (US), James B on 11/27/19.
// Copyright © 2019 Brent Michalski. All rights reserved.
//
import SwiftUI
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)
}
}
}
@perlguy99
Copy link
Author

This version seems to swallow up all events. Still testing.

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