简体   繁体   中英

How to show keyWindow in front of keyboard when app in background?

UIApplication.shared.keyWindow?.addSubview(self.backgroundImage)

I will try this one:

let windowCount = UIApplication.shared.windows.count

UIApplication.shared.windows[windowCount-1].addSubview(self.backgroundImage)

None

I type something with keyboard and press home buton. I need to show only background image when I return app but app shows keyboard in front of backgroundImage

The window contain keyboard is a single one and it's level is higher than the keyWindow. So your self.backgroundImage that in keyWindow can't cover keyboard.

Suggest:

call window.resignFirstResponder() when app will enter background.

Better way:

Add your backgroundImage to keyboard window if it is showing

if let keyboardWindow = applicationKeyboardWindow {
    keyboardWindow.addSubview(self.backgroundImage)
}

var applicationKeyboardWindow: UIWindow? {
   var windows = UIApplication.shared.windows.filter { win -> Bool in
        let descri = String(describing: type(of: win))
        return descri == "UITextEffectsWindow" || descri == "UIRemoteKeyboardWindow"
    }
    windows.sort(by: { $0.windowLevel > $1.windowLevel } )
    return windows.first
}

You can simply call resignFirstResponder() on the component (example: UITextField/UITextView ) for which you're showing the keyboard in viewWillDisappear(_:) , ie

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.textField.endEditing(true)
}

In the above code, replace self.textField with the specific input element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM