iOS why has moved ToolBar gap to keyboard

血红的双手。 提交于 2019-12-23 20:57:36

问题


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:

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

Thanks!


回答1:


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



回答2:


I figured out that the new position wasn't calculated right. Here my final code snippet to move the ToolBar with the keyboard within a Navigation Controller View (just the moving-up portion, view orientation added):

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

Attention: the keyboard.size.hight value hight is not adapting to landscape view.



来源:https://stackoverflow.com/questions/17412395/ios-why-has-moved-toolbar-gap-to-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!