3

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:

enter image description here

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

Thanks!

JFS
  • 2,992
  • 3
  • 37
  • 48
  • 1
    I would suggest to check if that space isn't actually part of the tool bar image. And also try to hard code first the position of the tool bar and after that check your computed values and compare (it's easier to find the issue). Also if you are using autolayout check the constraints. – danypata Jul 01 '13 at 22:39
  • Hello danypata. Thanks for your answers. 1. How can the space be part of the toolbar? The toolbar is part of my navigation controller. 2. I'll check that out. Good idea. 3. I'll check that as well but what could go wrong with the layouts? Thanks! – JFS Jul 02 '13 at 05:55

2 Answers2

2

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];
Martin Mandl
  • 721
  • 3
  • 11
  • Hello Martin, it looks like you like my questions... When I use you suggestion for the keyboard size I get exactly the same behavior. Its weird... – JFS Jul 01 '13 at 20:17
  • Well, you keep asking iOS questions, ... did you try instead of changing the frame of the toolbar to change the frame of its super view? – Martin Mandl Jul 01 '13 at 22:24
  • Ok, but how would that work? What would be the super view and how would I change it? – JFS Jul 02 '13 at 06:03
  • exchange self.navigationController.frame for self.navigationController.toolbar.frame ?? – Martin Mandl Jul 02 '13 at 08:41
  • OK, I got it. Your answer wasn't exactly solving my issue. I had some mistakes calculating a wrong position of the moved toolbar. But thanks for your help! – JFS Jul 02 '13 at 21:34
1

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.

JFS
  • 2,992
  • 3
  • 37
  • 48