I noticed while debugging view hierarchy that when keyboard is presented there's UIRemoteKeyboardWindow
in hierarchy.
First we can add extension to UIApplication to check window hierarchy for UIRemoteKeyboardWindow
:
extension UIApplication {
var isKeyboardPresented: Bool {
if let keyboardWindowClass = NSClassFromString("UIRemoteKeyboardWindow"), self.windows.contains(where: { $0.isKind(of: keyboardWindowClass) }) {
return true
} else {
return false
}
}
}
Then in viewDidLoad
, or where needed we can check:
if UIApplication.shared.isKeyboardPresented {
print("Keyboard is presented")
}
Although this method is not fully tested and UIRemoteKeyboardWindow
is in private headers that's why NSClassFromString
is needed for check. Use it with concern!