如何知道iOS 8中的当前interfaceOrientation?

时间:2015-01-15 15:27:00

标签: ios uiviewcontroller ios8 uikit uiinterfaceorientation

既然interfaceOrientation UIViewController中的{{1}}不推荐使用,那么检测当前设备方向的推荐方法是什么?

4 个答案:

答案 0 :(得分:11)

您可以使用[UIApplication sharedApplication].statusBarOrientation

请注意[[UIDevice currentDevice] orientation] 不完全,就像申请轮换一样。文档:

  

<强>讨论:   属性的值是一个常量,表示设备的当前方向。此值表示设备的物理方向,可能与应用程序用户界面的当前方向不同。有关可能值的说明,请参阅UIDeviceOrientation。

我会检查statusBarOrientation是否有效。

答案 1 :(得分:2)

UIApplication.sharedApplication()。statusBarOrientation有效,除非您从

调用它
func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)

预测对新statusBarOrientation的更改

因此,如果您使用viewWillTransitionToSize进行更新 基于方向(而不是大小等级)的设备,你被搞砸了 并且必须使用类似的东西

extension UIViewController {
    public var orientation : UIInterfaceOrientation? {
        let orientation = UIDevice.currentDevice().orientation
        var converted : UIInterfaceOrientation!
        // UIApplication.sharedApplication().statusBarOrientation is trash cause it lags (being set to the value predating the transition reported by viewWillTransitionToSize)
        switch orientation {
        case .Portrait: // Device oriented vertically, home button on the bottom
            converted = .Portrait
        case .PortraitUpsideDown: // Device oriented vertically, home button on the top
            converted = .PortraitUpsideDown
        case .LandscapeLeft: // Device oriented horizontally, home button on the right
            converted = .LandscapeLeft
        case .LandscapeRight: // Device oriented horizontally, home button on the left
            converted = .LandscapeRight
        default:
            // let the caller do the dirty dancing with the status bar orientation or whatever
            return nil
        }
        return converted
    }
}

您可以这样使用:

    // willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) is deprecated.
    // thusly I have to use the size classes method and compute interface orientation from the device orientation
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
        guard let converted = orientation else {
            //            alert("something else that I do not [need to] handle")
            return
        }
        if converted.isLandscape {
            adjustViewsForOrientation(converted, withTransitionCoordinator: coordinator)
        } else if converted.isPortrait {
            adjustViewsForOrientation(converted, withTransitionCoordinator: coordinator)
        }
    }

我不知道如何走出这个勇敢的新世界(吮吸) 令人信服的appple of undeprecating willRotateToInterfaceOrientation 与autoresizingMask相同的方式 (除非他们有意不要弃用autoresizingMask; - )

或者将interfaceorientation作为第三个参数注入到 viewWillTransitionToSize(size:CGSize,withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)

答案 2 :(得分:2)

在Swift中,您可以使用

获取它

let orientation: UIInterfaceOrientation = UIApplication.shared.statusBarOrientation

答案 3 :(得分:0)

for iPad

[[UIDevice currentDevice] orientation];