Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 11 - Keyboard Height is returning 0 in keyboard notification

I have been using Keyboard notifications without any problem and getting exact height of Keyboard.

- (void)keyboardDidShow:(NSNotification *) notification{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    NSLog(@"%f",keyboardSize.height);}

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

What is the problem occurring in this scenario? I am using xcode 9 Beta 5

like image 958
Hassy Avatar asked Aug 15 '17 08:08

Hassy


4 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
like image 167
s.zainulabideen Avatar answered Nov 13 '22 12:11

s.zainulabideen


Replace UIKeyboardFrameBeginUserInfoKey

with

UIKeyboardFrameEndUserInfoKey

The below is from Apple Docs.

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

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

like image 26
Aditya Srivastava Avatar answered Nov 13 '22 12:11

Aditya Srivastava


Try this:

Replace UIKeyboardFrameBeginUserInfoKey with UIKeyboardFrameEndUserInfoKey

like image 7
SolinLiu Avatar answered Nov 13 '22 12:11

SolinLiu


This issue is happening on iOS 11.

Replace

"UIKeyboardFrameBeginUserInfoKey" with "UIKeyboardFrameEndUserInfoKey"

as shown below would fix the issue

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
like image 6
Sujay U N Avatar answered Nov 13 '22 13:11

Sujay U N