Unable to get correct height of iPad floating keyboard

Viewed 116

I'm using the following code to get the height of the iPad keyboard:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(printKeyboardHeight),
            name: UIResponder.keyboardDidChangeFrameNotification,
            object: nil
        )
    }

    @objc private func printKeyboardHeight(notification: Notification) {
        guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
        print("==> keyboardFrameEndUserInfoKey height: \(keyboardRect.size.height)")
    }
}

This code on the iPad works correctly when the keyboard is normal but it does not work when the keyboard is floating.

After the user tap on a text field, the keyboard appears and the keyboardDidChangeFrameNotification notification is called.

If the keyboard is floating then the first time we get a wrong height of 362. If the user then manually moves somewhere the floating keyboard the notification is called again and the correct value of 308 is returned (see below image).

It is a bug or did I miss something? I need to be able to get the correct height the first time the keyboard appears.

This is happening on iOS 13 and iOS 14.

Any idea?

enter image description here

0 Answers
Related