One challenge we have found with SwiftUI is dismissing the keyboard programmatically. Our workaround is to use a function like this:
And then a helper in AppDelegate to get the currentWindow:
Is there a better way to do this, whether in SwiftUI with iOS 13 or now with iOS 14?
Code Block func dismissKeyboard() { AppDelegate.currentWindow?.endEditing(true) }
And then a helper in AppDelegate to get the currentWindow:
Code Block static var currentWindow: UIWindow? { UIApplication.shared.connectedScenes .filter { $0.activationState == .foregroundActive } .map { $0 as? UIWindowScene } .compactMap { $0 } .first?.windows .filter { $0.isKeyWindow }.first }
Is there a better way to do this, whether in SwiftUI with iOS 13 or now with iOS 14?