iOS 7 iPad 方向问题,当键盘出现在 UITextField 上后视图向上移动

iOS 7 iPad Orientation issue, when view moves upside after keyboard appears on UITextField

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

这必须支持所有 orientation.i.e:
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];
    }

谢谢

Gulp。我可以提一些建议吗?

首先,我会将事件附加到 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

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

呸! 希望这有帮助。