Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 886
Nitish Avatar asked Dec 10 '15 08:12

Nitish


1 Answers

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.

like image 74
Rahul Patel Avatar answered Oct 09 '22 09:10

Rahul Patel