【问题标题】:iOS 7 iPad Orientation issue, when view moves upside after keyboard appears on UITextFieldiOS 7 iPad 方向问题,当 UITextField 上出现键盘后视图向上移动时
【发布时间】:2015-03-29 22:35:48
【问题描述】:

在我的应用程序中,在 UITextField 上出现键盘后,我不想将 ViewController 的视图向上移动/动画。

这必须支持所有方向。即:
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight.

对于 UIInterfaceOrientationPortrait 我的代码工作正常,如下所示:

但对于 UIInterfaceOrientationPortraitUpsideDown 和 UIInterfaceOrientationLandscapeLeft 它不起作用。请看下面的截图:

对于 UIInterfaceOrientationLandscapeLeft 视图移动到右侧而不是向上侧

对于 UIInterfaceOrientationPortraitUpsideDown 视图移动到向下而不是向上

这只是 iOS 7 中的问题。我在 iOS 8 中检查过,它工作正常。

我使用以下代码在键盘外观上为 UIView 设置动画:

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 264;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 352;

    -(void) textFieldDidBeginEditing:(UITextField *)textField
    {
        CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];

        CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

        CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;

        CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
        CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
        CGFloat heightFraction = numerator / denominator;

        if (heightFraction < 0.0)
        {
            heightFraction = 0.0;
        }
        else if (heightFraction > 1.0)
        {
            heightFraction = 1.0;
        }

        UIInterfaceOrientation orientation =
        [[UIApplication sharedApplication] statusBarOrientation];
        if (orientation == UIInterfaceOrientationPortrait ||
            orientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
        }
        else
        {
            animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
        }

        if(self.view.frame.origin.y != 0.000000)
        {
            self.view.frame = CGRectMake(self.view.frame.origin.x,0.0,self.view.frame.size.width,self.view.frame.size.height);
        }

        CGRect viewFrame = self.view.frame;
        viewFrame.origin.y -= animatedDistance;

        NSLog(@"View frame y pos did start: %f ",animatedDistance);

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

        [self.view setFrame:viewFrame];

        [UIView commitAnimations];
    }

    -(void) textFieldDidEndEditing:(UITextField *)textField
    {
        CGRect viewFrame = self.view.frame;
        viewFrame.origin.y += animatedDistance;

        NSLog(@"View frame y pos did end: %f ",animatedDistance);

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

        [self.view setFrame:viewFrame];

        [UIView commitAnimations];
    }

谢谢

【问题讨论】:

    标签: ios iphone ipad ios7 ios8


    【解决方案1】:

    咕噜咕噜。我可以提些建议吗?

    首先,我会将事件附加到UIKeyboardWillShowNotificationUIKeyboardWillHideNotification,而不是使用 textFieldDidBeginEditing 事件。

    假设我在 iPad 上运行了您的应用,但连接了蓝牙键盘。我会点击文本框,你的UIView 会向上移动,但 iPad 不会显示屏幕键盘,因为它知道我不需要它。

    另一个优点是您目前正在为屏幕键盘使用硬编码的高度。哦,这不是一个好主意。

    看看我在这篇文章中的截图,举一个(只有一个!)为什么这不是一个好主意的例子:

    iPad keyboard height

    如果你选择走UIKeyboardWillShowNotification路线,你可以测量正确的键盘高度...

    -(void)onKeyboardWillShow:(NSNotification*)notification
    {
        //  The user has turned on the onscreen keyboard.
        //
        NSDictionary *info = [notification userInfo];
        NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardFrame = [kbFrame CGRectValue];
        keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
    
        CGFloat keyboardHeight = keyboardFrame.size.height;
    }
    

    这是我敲出的一个示例 XCode 项目,它演示了如何调整控件的大小,使其(几乎)填满整个屏幕,并在设备的方向和屏幕键盘可见性发生变化时调整它的大小:

    OnScreenKeyboardTest XCode 6.1 project

    您会看到它有一个“关闭”按钮,可以让屏幕键盘消失。此代码(应该)在所有 iPhone 和 iPad 上运行。

    呸! 希望这会有所帮助。

    【讨论】:

    • 感谢迈克的回答。您的解决方案对我很有帮助。
    • 酷!我在这个 XCode 地狱中发现了一件事......当你看到一个工作示例时,生活变得更加轻松!很高兴它有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2018-05-21
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2012-10-10
    相关资源
    最近更新 更多