iOS 8 方向更改:键盘框架无法正确显示 [英] iOS 8 Orientation change: Keyboard frame does not display correctly

查看:14
本文介绍了iOS 8 方向更改:键盘框架无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是 iOS 8 的问题,在 iOS 7 中键盘显示正确,设备方向发生了变化.我的应用程序支持纵向和横向,并使用自动布局.

This is only an iOS 8 problem, the keyboard displays correctly in iOS 7 with device orientation change. My application supports both portrait and landscape orientation and uses autolayout.

如果我将包含 UITextField 子类的 UIViewController 子类推送到导航堆栈上,并且 UITextField 成为纵向的第一响应者,则默认键盘会正确显示.但是,如果我将设备旋转到横向,则 UIViewController 子视图布局会正确显示,但键盘会显示在屏幕的顶部中心.键盘的方向对于横向方向是正确的,但它的框架宽度与纵向方向的预期宽度相同.如果我将应用程序方向设置为仅横向,则键盘无法正确显示.这只是 iOS 8 方向更改的问题.

If I push a UIViewController subclass that contains a UITextField subclass onto a navigation stack and the UITextField becomes the first responder in portrait orientation then the default keyboard displays correctly. However, if I rotate the device to landscape orientation then the UIViewController subview layouts are displayed correctly but the keyboard is displayed in the top center of the screen. The keyboard's orientation is correct for landscape orientation but it's frame width is the same width as expected for portrait orientation. If I set the app orientation to only landscape orientation then the keyboard does not display correctly. This is only a problem for iOS 8 orientation change.

推荐答案

在 iOS 8 之前,键盘的位置和宽度/高度在报告给应用程序时总是相对于纵向.(例如,横向的键盘宽度在 y 方向,在 iPad 上约为 352 像素.)从 iOS 8 开始,它已更新为始终在(物理)视图的左上角显示 (0,0),并且宽度/高度反映了您通常在 iOS 之外所期望的 x/y 方向.如果您之前通过 keyboardDidShow[notification userInfo] 之类的方式定位键盘,您将获得不太有意义的数字.您可以使用这些方法来考虑 iOS8 之前的特性:

Prior to iOS 8, the keyboard's location and width/height were always relative to portrait orientation when reported to the app. (e.g. Landscape's keyboard width is in the y direction, ~352 pixels on an iPad.) As of iOS 8, this has been updated to always have (0,0) at the top left of your (physical) view and the width/height reflect the x/y orientation you would normally expect outside of iOS. If you were previously positioning your keyboard via something like keyboardDidShow's [notification userInfo], you are going to get numbers that don't quite make sense. You can use something along these lines to take into account the pre-iOS8 idiosyncrasies:

- (void)keyboardDidShow: (NSNotification *) notification{

    NSDictionary *keyboardInfo = [notification userInfo];
    CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    float height, width;
    if(UIInterfaceOrientationIsPortrait(orientation)){
        width = keyboardSize.width;
        height = keyboardSize.height;
    } else {
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1){
            width = keyboardSize.height;
            height = keyboardSize.width;
        } else {
            width = keyboardSize.width;
            height = keyboardSize.height;
        }
    }

    // Remainder of function
}

可以重构为...

- (void)keyboardDidShow: (NSNotification *) notification{

    NSDictionary *keyboardInfo = [notification userInfo];
    CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    float width = keyboardSize.width;
    float height = keyboardSize.height;
    if(!UIInterfaceOrientationIsPortrait(orientation) && (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1)){
        width = keyboardSize.height;
        height = keyboardSize.width;
    }

    // Remainder of function
}

此外,8.1 更新修复了几个可能与上述更改相关的横向/旋转错误.获取更新,看看是否能解决您的问题.

Also, the 8.1 update fixed several landscape/rotation bugs likely related to the above change. Grab the update and see if that solves your issue.

这篇关于iOS 8 方向更改:键盘框架无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆