0

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

Simon
  • 6,413
  • 6
  • 34
  • 57
  • Take a look at the answer with 100+ upvotes here: https://stackoverflow.com/questions/25874975/cant-get-correct-value-of-keyboard-height-in-ios8 – DonMag Jun 08 '17 at 18:48
  • Besides using the End key instead of the Begin, you should also be using `UIKeyboardWillChangeFrame`, otherwise you won't handle the case of the user switching between keyboard (with different sizes) while the keyboard is visible. – Lily Ballard Jun 08 '17 at 19:47

0 Answers0