Search code examples
iosuitoolbarkeyboard-events

iOS why has moved ToolBar gap to keyboard


I figured out how to move my toolbar with button and text field with the appearing keyboard:

- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification
{
NSDictionary* userInfo = [aNotification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardFrame;

[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];


[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];    

[self.navigationController.toolbar setFrame:CGRectMake(self.navigationController.toolbar.frame.origin.x,
                                                       self.navigationController.toolbar.frame.origin.y - keyboardFrame.size.height +self.navigationController.toolbar.frame.size.height,
                                                       self.navigationController.toolbar.frame.size.width,
                                                       self.navigationController.toolbar.frame.size.height)];
[UIView commitAnimations];

}

Everything works fine but there is a small gap between the moved toolbar and the keyboard:

enter image description here

and I can't figure out the problem? What could be the problem or is that the expected behavior?

Thanks!


Solution

  • Try the following for calculating the new frame size:

    CGRect kbFrameBegin;
    [[userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &kbFrameBegin];
    CGRect kbFrameEnd;
    [[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &kbFrameEnd];    
    CGRect frame = self.navigationController.toolbar.frame;    
    frame.size.height -= abs(kbFrameBegin.origin.y - kbFrameEnd.origin.y);
    [self.navigationController.toolbar setFrame:frame];