Nitish
Nitish

Reputation: 14113

UIKeyboardWillShowNotification returning incorrect frame for swift keyboard

I am using UIKeyboardWillShowNotification and UIKeyboardWillHideNotification to handle keyboard.
This is the function called when keyboard is shown :

-(void) keyboardWillShow:(NSNotification *)note
{
    CGRect keyboardBounds;
    //[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSValue* keyboardFrameBegin = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    keyboardBounds = [keyboardFrameBegin CGRectValue];  
}  

I get keyboardBounds = (0, 524, 320, 44) and I am using 5s. No idea why origin.y is coming 524 (should be somewhere near 300) and height as 44 !! I have also tried the commented line. Both ways keyboard bound comes out to be 44. This issue comes only for swift keyboard.
Same as this issue

Upvotes: 15

Views: 2820

Answers (2)

lichao
lichao

Reputation: 29

Maybe you can try other notifications like:UIKeyboardDidShowNotificationUIKeyboardDidChangeFrameNotification. UIKeyboardWillShowNotification

Just tell you the keyboard will show to you ,can not tell the exactly key board shown frame.

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 1832

Well the issue is with swift keyboard, the method -(void) keyboardWillShow:(NSNotification *)note is called three times, and every time it returns three different origin.y value and height first time it gives keyboardBounds = (0, 524, 320, 44), second time it it gives keyboardBounds = (0, 308, 320, 260) and finally the third time when it is called it returns keyboardBounds = (0, 271, 320, 297).

As third party keyboard sizes are not fixed.. their sizes get fixed as per how view lay it out, so as similar to autolayout case(viewDidLoad,viewWillAppear viewWillLayout,viewDidLayout and then viewDidAppear, so exact frame you get to know in viewDidAppear or viewDidLayout, where View have been laid out.), Here it gets exact frame when it's view get completely laid out.

Upvotes: 2

Related Questions