I'm using the following code to get the height of the keyboard:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardHeight),
name: UIResponder.keyboardDidChangeFrameNotification,
object: nil
)
}
@objc private func keyboardHeight(notification: Notification) {
guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
print("==> keyboardFrameEndUserInfoKey height: \(keyboardRect.size.height)")
}
}
This code on iPad work 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 appear 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 move somewhere the floating keyboard the notification is called again and the correct value of 308 is returned.
It is a bug or I miss something? I need to be able to get the correct height the first time the keyboard appear.
This is happening on iOS 13 and iOS 14.
Any idea?