iOS为什么将ToolBar差距移到键盘上

时间:2013-07-01 19:42:01

标签: ios uitoolbar keyboard-events

我想出了如何使用显示的键盘移动带有按钮和文本字段的工具栏:

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

}

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

enter image description here

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

谢谢!

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

我发现新职位没有正确计算。这是我的最终代码片段,用于在导航控制器视图中使用键盘移动ToolBar(只是向上移动部分,添加了视图方向)​​:

- (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值高度不适应横向视图。