3

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

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

}

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

在此处输入图像描述

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

谢谢!

4

2 回答 2

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];
于 2013-07-01T19:56:43.767 回答
1

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

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

于 2013-07-02T21:41:19.990 回答