I have a textfield that is covered when the keyboard pops up. To account for this I wrap the content view inside a scroll view and adjust its height when the keyboard is shown.
I set the observers in viewDidLoad:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
And the methods that show/hide the keyboard.
func keyboardWillShow(notification: NSNotification) {
adjustInsetForKeyboardShow(show: true, notification: notification)
}
func keyboardWillHide(notification: NSNotification) {
adjustInsetForKeyboardShow(show: false, notification: notification)
}
private func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let adjustmentHeight = ((keyboardFrame.height) * (show ? 1 : -1))
scrollView.contentInset.bottom += adjustmentHeight
scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}
On the simulator/device when the default
iOS keyboard is used this seems to be working properly.
However, when I use a third party keyboard (more specifically GBoard
), the scroll view adjusts the height to be significantly more than what the default
keyboard and stretches the view with a lot of white space.
Does the notification.userInfo
property account for third party keyboards for the key UIKeyboardFrameBeginUserInfoKey
?
I am not sure what is going on