I've spent a lot of time researching and playing with different codes to make this work but can't seem to figure it out. Below you'll see two pieces of code, one of me creating the "Done" bar button item and the other is checking to see if keyboard is present and to close it when the button is clicked. The one issue I'm having is, I only want the bar button to show when the keyboard is present. I want the bar button hidden once the keyboard is gone or prior to even opening up the keyboard in the first place. How do you do that nowadays with SwiftUI?
.navigationBarItems(trailing:
Button(action: {
if UIApplication.shared.isKeyboardPresented {
UIApplication.shared.endEditing()
}
}, label: {
Text("Done")
})
extension UIApplication {
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
/// Checks if view hierarchy of application contains `UIRemoteKeyboardWindow` if it does, keyboard is presented
var isKeyboardPresented: Bool {
if let keyboardWindowClass = NSClassFromString("UIRemoteKeyboardWindow"),
self.windows.contains(where: { $0.isKind(of: keyboardWindowClass) }) {
return true
} else {
return false
}
}
}