iOS在UINavigationController中使用键盘移动ToolBar

时间:2013-06-30 14:00:38

标签: ios keyboard toolbar

我疯了。当键盘出现时,我会尝试将ToolBar与其文本字段一起移动。使用以下代码,视图实际上正在移动,但ToolBar保持并被键盘隐藏:

- (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:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];

[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.size.height)];

[UIView commitAnimations];
}

我是初学者,所以请不要打败我,但我不知道移动的缺失链接并显示ToolBar及其文本字段。

工具栏和文本字段是在viewDidLoad方法中创建的。

2 个答案:

答案 0 :(得分:4)

我发现ToolBar本身必须随键盘移动: 这可以防止它被显示的键盘隐藏:

enter image description here

在NavigationController中使用ToolBar时,下面的代码可以解决这个问题:

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

}

所有其他必要的代码都有很好的描述HERE

答案 1 :(得分:1)

self.view不包含工具栏,您必须单独移动工具栏...