iPhone 6 Plus出现方向不正确的问题?

3
我希望我的应用程序在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个回答

2
我们可以采用多种方法来设置适当的界面方向。首先,使用硬编码高度容易出现错误,而苹果强烈反对这种设备检查方式。相反,我们将使用trait集合。UITraitCollection是iOS 8中引入的API,它包含有关设备特征、显示比例和大小类的信息。您可以在UIWindow和UIViewController对象上访问trait集合。
在我们的示例中,我们将首先使用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文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接