iOS为什么已经将ToolBar与键盘间隔开了

我想通过出现的键盘如何移动我的工具栏与button和文本字段:

- (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]; } 

一切工作正常,但移动的工具栏和键盘之间有一个小的差距:

在这里输入图像说明

我无法弄清楚这个问题? 可能是什么问题或者是预期的行为?

谢谢!

尝试以下计算新的帧大小:

 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]; 

我发现这个新的位置不是正确的。 在这里,我的最后一个代码片段,用导航控制器视图中的键盘移动工具栏(只是移动部分,添加了视图方向)​​:

 - (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification { NSDictionary* userInfo = [aNotification userInfo]; NSTimeInterval animationDuration; UIViewAnimationCurve animationCurve; CGRect keyboardFrame; CGFloat keyboardHeight; [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame]; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown ) { keyboardHeight = keyboardFrame.size.height; } else { keyboardHeight = keyboardFrame.size.width; } [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:animationDuration]; [UIView setAnimationCurve:animationCurve]; [self.navigationController.toolbar setFrame:CGRectMake(self.navigationController.view.frame.origin.x, self.navigationController.view.frame.origin.y + self.navigationController.view.frame.size.height + self.tabBarController.tabBar.frame.size.height - keyboardHeight - self.navigationController.toolbar.frame.size.height, self.navigationController.toolbar.frame.size.width, self.navigationController.toolbar.frame.size.height)]; [UIView commitAnimations]; NSLog(@"toolbar moved: %f", self.navigationController.view.frame.size.height); } 

注意: keyboard.size.hight值不适用于横向视图。