当键盘出现在iPad上时,如何向上移动视图?

当键盘出现在iPad上时,如何向上移动视图?,ipad,uikeyboard,Ipad,Uikeyboard,当iPad虚拟键盘出现时,它覆盖了我视图中的一些文本字段。有没有办法在键盘出现时向上移动ViewController?我的建议是将对象放置在UIScrollView上,然后当键盘出现时,可以使用scrollRectToVisible: [scroller scrollRectToVisible:frame animated:YES]; 我已经为我的一个应用程序编写了这段代码 它会自动检测文本字段的位置,并相应地滚动baseView - (void)textFieldDidBeginEdi

当iPad虚拟键盘出现时,它覆盖了我视图中的一些文本字段。有没有办法在键盘出现时向上移动ViewController?

我的建议是将对象放置在
UIScrollView
上,然后当键盘出现时,可以使用
scrollRectToVisible:

[scroller scrollRectToVisible:frame animated:YES];

我已经为我的一个应用程序编写了这段代码

它会自动检测文本字段的位置,并相应地滚动baseView




- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField:textField up:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField:textField up:NO];
}



- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil];
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait){

        if(up) {
            int moveUpValue = temp.y+textField.frame.size.height;
            animatedDis = 264-(1024-moveUpValue-5);
        }
    }
    else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {
        if(up) {
            int moveUpValue = 1004-temp.y+textField.frame.size.height;
            animatedDis = 264-(1004-moveUpValue-5);
        }
    }
    else if(orientation == UIInterfaceOrientationLandscapeLeft) {
        if(up) {
            int moveUpValue = temp.x+textField.frame.size.height;
            animatedDis = 352-(768-moveUpValue-5);
        }
    }
    else
    {
        if(up) {
            int moveUpValue = 768-temp.x+textField.frame.size.height;
            animatedDis = 352-(768-moveUpValue-5);
        }

    }
    if(animatedDis>0)
    {
        const int movementDistance = animatedDis;
        const float movementDuration = 0.3f; 
        int movement = (up ? -movementDistance : movementDistance);
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        if (orientation == UIInterfaceOrientationPortrait){
            baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);       
        }
        else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {

            baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
        }
        else if(orientation == UIInterfaceOrientationLandscapeLeft) {

            baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
        }
        else {
            baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
        }

        [UIView commitAnimations];
    }
}


请将ValayPatel代码“if”语句更改为
if(animatedDis>0 | | | |!up)
条件,当up=NO时,否则移动的视图始终位于前一位置。

我对这方面有点陌生,您有一个简单的示例说明如何做到这一点吗?谢谢谢谢,我真的很感激。我相信原始代码打算将animatedDis作为一个属性,因此在完成编辑时将保留>0值,从而使视图动画化到其原始位置。确实非常有用。试过了。工作完美。谢谢:)