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
Use this:
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
For Swift, you can use:
let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
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.
Try this:
Replace UIKeyboardFrameBeginUserInfoKey
with UIKeyboardFrameEndUserInfoKey
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With