繁体   English   中英

如何以纵向方向移动键盘视图

[英]How to move view for keyboard in portrait orientations

我有一个仅在portrait和portraitUpsideDown中运行的应用程序。 我需要在键盘出现时将视图向上推,在键盘消失时将其拉回。 如果设备保持纵向,下面的代码可以完美地工作,但是如果设备处于portraitUpsideDown,则视图将向错误的方向移动(-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){
    }];
}

我通过以下方式解决了它(不太优雅):

注意:您应该执行的操作,最终将更改此代码以执行以下操作:创建一个属性以保存键盘动画的持续时间,以便可以在键盘委托方法之外使用它,并类似地为offset创建一个属性并确定它使用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){
        }];
    }
}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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