How to check in which position (landscape or portrait) is the iPhone now?

后端 未结 13 1202
旧巷少年郎
旧巷少年郎 2021-01-30 06:47

I have an app with a tab bar, and nav controllers in each tab. When user shakes the device, a UIImageView appears as a child view in the nav controller. But the

13条回答
  •  我在风中等你
    2021-01-30 07:40

    Try this:

    [[UIApplication sharedApplication] statusBarOrientation]
    

    Or in Swift 3:

    UIApplication.shared.statusBarOrientation
    

    To specifically check for a particular orientation you can also try the isLandscape or isPortrait property like so:

    UIApplication.shared.statusBarOrientation.isLandscape
    

    The problem with [[UIDevice currentDevice] orientation] is that it will also return UIInterfaceOrientationUnknown which statusBarOrientation does not.

    There is also a UIViewController property interfaceOrientation but it was deprecated in iOS 8, so it's not recommended.

    You check documentation for statusBarOrientation here

提交回复
热议问题