iOS的8方向的变化:键盘边框显示不正确 [英] iOS 8 Orientation change: Keyboard frame does not display correctly

查看:211
本文介绍了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子类到导航堆栈和的UITextField变成纵向的第一个响应者则默认的键盘显示正确一个UIViewController子类。但是,如果我旋转设备为横向则的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)你(物理)视图的左上角和宽度/高度反映了X / Y方向,你通常会想到的iOS之外。如果您在previously定位通过一些你的键盘就像 keyboardDidShow [通知用户信息] ,你会得到数字,不太意义。您可以使用的东西沿着这些线路考虑到pre-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
}

由此可重构下降到...

Which can be refactored down to...

- (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天全站免登陆