【问题标题】:Incorrect orientations on iPhone 6 Plus?iPhone 6 Plus 上的方向不正确?
【发布时间】:2016-06-03 21:07:52
【问题描述】:

我希望我的应用可以在 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
    }

}

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

【问题讨论】:

    标签: ios iphone swift ipad orientation


    【解决方案1】:

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

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

    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
            }
        }
    

    如果您想了解更多关于 trait 集合和大小类的信息,我建议您阅读官方 Apple documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2015-01-19
      相关资源
      最近更新 更多