0

我有一个仅以纵向和纵向向上运行的应用程序。我需要在键盘出现时将视图向上推,并在它消失时将其拉回。如果设备保持纵向,则以下代码可以完美运行,但如果它是纵向的,则视图会移动错误的方向(-260 而不是 260),而且如果在显示键盘时方向发生变化,则不会处理...... keyboardWillHide 方法在两个方向都可以正常工作。有没有办法将视图相对移动到键盘或状态栏,所以设备的方向无关紧要???

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

3 回答 3

0

不幸的是,您将不得不自己管理。您可以询问当前方向并适当地设置 y 轴调整。

int movementDistance = -260;
if (UIInterfaceOrientationPortraitUpsideDown == [self interfaceOrientation]) movementDistance = -movementDistance;
于 2012-05-09T19:55:15.453 回答
0

我以这种方式解决了它(不太优雅):

注意:您应该做的事情,我将更改此代码以最终执行它们是:创建一个属性来保存键盘动画持续时间,以便我可以在键盘委托方法之外使用它,并类似地为偏移量创建一个属性并确定它使用 userInfo 作为键盘的高度。

- (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){
        }];
    }
}
于 2012-05-09T19:54:02.470 回答
-1

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

于 2012-05-09T19:14:53.007 回答