开发者

move up UIToolbar

I have a UIToolbar at bottom of my view, and I have a UITextField in this toolbar. When I begin editing this field, it is hidden behind the keyboard. To see what I've typed, I want to move the toolbar up at the moment the keyboard is presente开发者_如何转开发d (and then move it back down when I've finished editing).

How do I move this UIToolbar up/down?


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


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


Building on the answers above and using the convenience method [UIView animateWithDuration...]. Observe the will show/hide keyboard notifications and use these handlers.

- (void)keyboardWillShow:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey];

    [UIView animateWithDuration:durationValue.doubleValue
                          delay:0
                        options:(curveValue.intValue << 16)
                     animations:^{
                         self.navigationController.toolbar.frame = CGRectMake(0,
                                                                              [endFrame CGRectValue].origin.y - self.navigationController.toolbar.bounds.size.height,
                                                                              self.navigationController.toolbar.bounds.size.width,
                                                                              self.navigationController.toolbar.bounds.size.height);
                     }
                     completion:nil];
}

- (void)keyboardWillHide:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];

    [UIView animateWithDuration:durationValue.doubleValue
                          delay:0
                        options:(curveValue.intValue << 16)
                     animations:^{
                         self.navigationController.toolbar.frame = CGRectMake(0,
                                                                              self.view.bounds.size.height - self.navigationController.toolbar.bounds.size.height,
                                                                              self.navigationController.toolbar.bounds.size.width,
                                                                              self.navigationController.toolbar.bounds.size.height);
                     }
                     completion:nil];
}


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


Here is a more elegant solution using uiview category

#import <Foundation/Foundation.h>


@interface UIView(AnimationUtils)
-(void)scrollControlToCenter:(UIView *)view;
-(void)scrollViewToOriginalPosition;
@end



#import "UIView+AnimationUtils.h"


@implementation UIView(AnimationUtils)
#pragma mark ScrollView Methods

//Method Called whenever keyboard appears
- (void)scrollControlToCenter:(UIView *)view {  
    if([view isKindOfClass:[UITextField class]]){
        CGRect viewFrame = [view frame];
        float verticalDistance = 216.0f - viewFrame.origin.y - (2*viewFrame.size.height);
        if(viewFrame.size.height >= (460 - 216)/2){
            verticalDistance = 0;
        }
        [UIView beginAnimations:@"ScrollToCenter" context:nil];
        [UIView setAnimationDuration:0.5];
        [self setFrame:CGRectMake(0, verticalDistance, self.frame.size.width, self.frame.size.height)];
        [UIView commitAnimations];
    }else if([view isKindOfClass:[UITextView class]]){
        [UIView beginAnimations:@"ScrollToTop" context:nil];
        [UIView setAnimationDuration:0.5];
        UIView *viewBG = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        [viewBG setTag:5];
        [viewBG setBackgroundColor:[UIColor blackColor]];
        [viewBG setAlpha:0.75];
        [self addSubview:viewBG];
        [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
        [self setFrame:CGRectMake(0, -view.frame.origin.y , self.frame.size.width, self.frame.size.height)];
        [self insertSubview:view atIndex:[self.subviews count] + 1];
        [UIView commitAnimations];
    }

}
-(void)scrollViewToOriginalPosition{
    [UIView beginAnimations:@"ScrollToOriginal" context:nil];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in self.subviews){
        if(view.tag == 5){
            [view removeFromSuperview];
        }
    }
    [self setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [UIView commitAnimations];  

}

#pragma mark -
@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜