简体   繁体   中英

How to move view for keyboard in portrait orientations

I have an app that runs in portrait and portraitUpsideDown only. I need to push the view up when the keyboard appears and pull it back down when it disappears. The following code works perfectly if the device stays in portrait, but if it is in portraitUpsideDown the view moves the wrong direction (-260 instead of 260), plus if the orientation changes while the keyboard is showing, it's not handled... The keyboardWillHide method works fine in both orientations. Is there a way to move the view RELATIVE to the keyboard or status bar so it doesn't matter what orientation the device is in???

- (void) keyboardWillShow:(NSNotification *) notification
{
    NSLog(@"Keyboard Will Show");
    double animationDuration;
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + -260);
    }completion:^(BOOL finished){
    }];
}

- (void) keyboardWillHide:(NSNotification *) notification
{
    double animationDuration;
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSLog(@"Keyboard Will Hide");

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
        [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
     }completion:^(BOOL finished){
    }];
}

I solved it (less than elegantly) this way:

NOTE: Things you SHOULD do, and I will change this code to do them eventually are: create a property to hold the keyboard animation duration so I can use it outside of the keyboard delegate methods, and similarly create a property for the offset and determine it using the userInfo for the height of the keyboard.

- (void) keyboardWillShow:(NSNotification *) notification
{
    NSLog(@"Keyboard Will Show");
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
        offset = -260;
    else 
        offset = 260;

    double animationDuration;
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + offset);
    }completion:^(BOOL finished){
    }];
}

- (void) keyboardWillHide:(NSNotification *) notification
{
    double animationDuration;
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSLog(@"Keyboard Will Hide");

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y - offset);
     }completion:^(BOOL finished){
    }];
}

- (void) keyboardDidShow
{
    NSLog(@"Keyboard Did Show");
    keyboardIsShowing = YES;
}

- (void) keyboardDidHide
{
    NSLog(@"Keyboard Did Hide");
    keyboardIsShowing = NO;
}


-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{    
    if (keyboardIsShowing && UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        if (toInterfaceOrientation == UIInterfaceOrientationPortrait && offset == 260)
            offset = -260;
        else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown && offset == -260)
            offset = 260;
        else 
            return;
        [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
            self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 2* offset);
        }completion:^(BOOL finished){
        }];
    }
}

Unfortunately you will have to manage this yourself. You can interrogate the current orientation and set your y-axis adjustment appropriately.

int movementDistance = -260;
if (UIInterfaceOrientationPortraitUpsideDown == [self interfaceOrientation]) movementDistance = -movementDistance;

您需要设置框架高度,或为视图而不是中心设置contentInset。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM