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

发布于 2024-10-12 03:03:09 字数 71 浏览 2 评论 0原文

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

When the iPad virtual keyboard appears, it covers some of the textfields in my View. Is there a way to move the ViewController up when the keyboard appears?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅语花开 2024-10-19 03:03:09

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

它会自动检测 TextField 的位置并相应地滚动 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];
    }
}


I have wrote this code for one of my Application.

It automatically detects the Position of the TextField and Scroll the baseView Accordingly.




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


白日梦 2024-10-19 03:03:09

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

[scroller scrollRectToVisible:frame animated:YES];

My suggestion is to place your objects on a UIScrollView, then when the keyboard appears, you can use scrollRectToVisible:.

[scroller scrollRectToVisible:frame animated:YES];
软甜啾 2024-10-19 03:03:09

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

Do change ValayPatel code "if" statement to if(animatedDis>0 || !up) for condition when up = NO, else the shifted view is always at previous position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文