views:

874

answers:

3
+1  Q: 

move up UIToolbar

I have UIToolbar at bottom of my view. And I have UITextField in this toolbar. When I begin editing this field keyboard hide it. And to see what i type I want to move toolbar up at the moment of showing keyboard (and move down then finish editing).

How to move up/down UIToolbar?

A: 

add your viewController class to the list of observers of UIKeyboardWillShowNotification/UIKeyboardWillHideNotification. then you can move your view to make your textView visible. You can also get animation parameters from this notifications to synchronize your animation with keyboard animation parameters of the current OS version. this code I've used for paging

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(liftMainViewWhenKeybordAppears:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(returnMainViewToInitialposition:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

in the methods below I set two methods to handle keyboard notifications. and here are this methods:

- (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:UIKeyboardBoundsUserInfoKey] 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];
}
- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{
    NSDictionary* userInfo = [aNotification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardFrame;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] 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];
}
Morion
+2  A: 

Thanks that worked! Here is a slight improvement:

- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{
    [self scrollViewForKeyboard:aNotification up:YES];
}

- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{
    [self scrollViewForKeyboard:aNotification up:NO];
}

- (void) scrollViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up{
    NSDictionary* userInfo = [aNotification userInfo];

    // Get animation info from userInfo
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardFrame;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame];

    // Animate up or down
    [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 * (up?-1:1)), self.view.frame.size.width, self.view.frame.size.height)];
    [UIView commitAnimations];
}
Manuel Darveau
+1  A: 

Thank you so much for this; it works great. However, the code as presented has 2 limitations as I experienced them:

1) The view being repositioned simply slides up out of the screen rather than resizing to fit the space available after the keyboard appears

2) Repeat notifications due to switching text fields continue to apply the frame change, causing the view to incrementally fly off the screen.

The cause is that the above is a reposition relative to the view's current frame rather than a resize relative to the keyboard. Here are two amended lines of code which fix this:

In liftMainViewWhenKeybordAppears:, resize rather than reposition, relative to the keyboard:

    keyboardFrame = [self.view.window convertRect:keyboardFrame toView:self.view.superview];
    CGRect superviewFrame = [self.view.window convertRect:self.view.superview.frame toView:self.view];
    [self.view setFrame:CGRectMake(self.view.frame.origin.x, 
                               self.view.frame.origin.y,
                               self.view.frame.size.width, 
                               superviewFrame.size.height - keyboardFrame.size.height)];

In returnMainViewToInitialposition:, change the animation to this setFrame: (essentially akin to the identity transform).

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