如何在纵向方向上移动键盘视图

时间:2012-05-09 18:51:38

标签: objective-c ios ipad

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

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

3 个答案:

答案 0 :(得分: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){
        }];
    }
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:-1)

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