IOS 11 - Keyboard Height Returns 0 in Keyboard Notification

I use keyboard notifications without any problems and get the exact height of the keyboard.

- (void)keyboardDidShow:(NSNotification *) notification{ CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; NSLog(@"%f",keyboardSize.height);} 

but with iOS 11, the keyboard size is 0 when the notification is called.

What is the problem arising in this scenario? I am using xcode 9 beta 5

+79
ios objective-c nsnotificationcenter
Aug 15 '17 at 8:59
source share
7 answers

Use this:

 CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

For Swift, you can use:

 let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size 
+176
Aug 15 '17 at 9:04 on
source share

Replace UIKeyboardFrameBeginUserInfoKey

from

UIKeyboardFrameEndUserInfoKey

Following are Apple docs.

UIKeyboardFrameBeginUserInfoKey - a key for an NSValue object containing a CGRect that identifies the initial frame of the keyboard in screen coordinates.

UIKeyboardFrameEndUserInfoKey - a key for an NSValue object containing a CGRect that identifies the final frame of the keyboard in screen coordinates.

+98
Aug 15 '17 at 9:04 on
source share

Try the following:

Replace UIKeyboardFrameBeginUserInfoKey with UIKeyboardFrameEndUserInfoKey

+7
Aug 15 '17 at 9:16 on
source share

I had a similar problem using Xcode Version 9.0 (9A235); although I used Swift. In my keyboardWillShow method, I wrote the following:

 if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { let heightValue = keyboardSize.height ... } 

Oddly enough, the first time the WillShow keyboard was called, heightValue was 216.0, but during subsequent calls it got 0! Perhaps this is an Xcode error.

I replaced UIKeyboardFrameBeginUserInfoKey with UIKeyboardFrameEndUserInfoKey and it fixed this problem for me.

+5
Sep 29 '17 at 16:10
source share

This issue occurs on iOS 11.

Replace

"UIKeyboardFrameBeginUserInfoKey" with "UIKeyboardFrameEndUserInfoKey"

as shown below, the problem will be fixed

Objective-C Code:

 CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

Swift 2.3:

 let keyboardSize = (NfnPsgVar.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue().size 

Swift 3:

 let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size 
+2
Dec 20 '17 at 19:33
source share

Your approach is trying to get the frame height before it is shown, and that is why it should be equal to 0, which I'm not sure why this is not the first try! Here is an example of how to get keyboard height correctly in Swift 4.2:

 func keyboardWillShow(notification: Notification) { guard let userInfo = notification.userInfo else { return } guard var keyboardFrame: CGRect = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return } keyboardFrame = view.convert(keyboardFrame, from: nil) let keyboardHeight = keyboardFrame.height } 

This will correctly provide the properties of the keyboard frame BEFORE the keyboard appears.

+1
Jan 08 '19 at 22:53
source share

Calculate the height of the keyboard using the code below. It works for both devices with safe area and non-safe area devices.

 @objc func keyboardWillShow(notification: Notification) { guard let keyboardFrame = notification.userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return } let keyboardHeight: CGFloat if #available(iOS 11.0, *) { let window = UIApplication.shared.keyWindow let bottomPadding = window?.safeAreaInsets.bottom ?? 0.0 keyboardHeight = keyboardFrame.cgRectValue.height - bottomPadding } else { keyboardHeight = keyboardFrame.cgRectValue.height } } 
+1
Feb 21 '19 at 7:10
source share



All Articles