iPhone 6 Plus上的方向不正确?

时间:2016-06-03 21:07:53

标签: ios iphone swift ipad orientation

我希望我的应用程序可以在iPad上的所有方向上工作,在iPhone 6 Plus上支持横向和纵向,仅在其他设备上支持纵向。

但它在iPhone 6 / 6s Plus上无法正常工作。旋转很奇怪,视图控制器经常出现错误的方向。

这是我目前在AppDelegate.swift

中的内容
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {

    let height = window?.bounds.height

    if height > 736.0 {
        // iPad
        return .All
    } else if height == 736.0 {
        // 5.5" iPhones
        return .AllButUpsideDown
    } else {
        // 4.7", 4", 3.5" iPhones
        return .Portrait
    }

}

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

我们可以使用多种方法来设置适当的界面方向。首先,使用硬编码高度容易出错,Apple强烈反对这种类型的设备检查。相反,我们将使用特质集合。 UITraitCollection是iOS 8中引入的API,它包含有关设备习惯用法,显示比例和大小类的信息。您可以访问UIWindowUIViewController个对象上的特征集合。

在我们的示例中,我们将首先使用userInterfaceIdiom属性检查设备是否为iPad,然后我们将检查iPhone 6 / 6s Plus的displayScale(3.0)。

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {

        if window?.traitCollection.userInterfaceIdiom == .Pad {
            // Check for iPad
            return .All
        } else if window?.traitCollection.displayScale == 3.0 {
            // iPhone 6/6s Plus is currently only iPhone with display scale of 3.0
            return [.Portrait, .Landscape]
        } else {
            // Return Portrait for all other devices
            return .Portrait
        }
    }

如果您想了解更多有关特质收藏品和尺码类的信息,我建议您阅读官方Apple documentation