One challenge we have found with SwiftUI is dismissing the keyboard programmatically. Our workaround is to use a function like this:
func dismissKeyboard() {
AppDelegate.currentWindow?.endEditing(true)
}
And then a helper in AppDelegate to get the currentWindow:
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?
Post
Replies
Boosts
Views
Activity
As with most apps, our apps need to allow the user to immediately type in a specific field when a view loads for the best experience. We currently have to use a UIViewRepresentable to make this possible. I was sure it would get resolved with the next release to SwiftUI, but unless I've missed it, this is still not possible.
In the Build SwiftUI apps for tvOS - https://developer.apple.com/videos/play/wwdc2020/10042/ session, I saw prefersDefaultFocus(_:in:) - https://developer.apple.com/documentation/swiftui/button/prefersdefaultfocus(_:in:) (<= markdown parsing failure) which looked promising. But it is only available for tvOS and watchOS.
Has anyone figured out how to make a TextField or TextEditor become first responder when its view is shown?
From the Designing with iOS Menus - https://developer.apple.com/videos/play/wwdc2020/10205 and Building with iOS Menus - https://developer.apple.com/videos/play/wwdc2020/10052/, it was clear that we should replacing many of our action sheets with menus. I couldn't find how to do that with SwiftUI though.
The only thing that I am seeing is still .contextMenu, which would appear after a long press. Am I missing something, or does SwiftUI lack support for this new approach?