Search code examples
iosswiftkeyboardframeworksuiwindow

How to hide Keyboard which is in another window? Swift


I have a framework, which has a new window and and in that window, I have a button. Now I have imported that framework in my Host app, and now I have a floating button. So now when I tap on that button, an alert appears from the bottom. But the problem comes, when the keyboard is open in the host app window and I try to click that button, the alert opens but is hidden behind the keyboard. How do I dismiss that keyboard as soon as button is clicked? How can I dismiss keyboard of a window from anther window? Or this will also work for me if I can set my framework window level higher than the keyboard window level. Any help would be appreciated.


Solution

  • First, get a reference to host app keyWindow

      func getHostKeyWindow() -> UIWindow? {
        if #available(iOS 13, *) {
            return windows.first { $0.isKeyWindow }
        } else {
            return keyWindow
        }
    }
    

    And then simply make endEditing as true

    UIApplication.shared.getHostKeyWindow()?.endEditing(true)
    

    It should close the keyboard in the window for which you have the reference!