uiinterfaceorientationuideviceorientation

When is statusBarOrientation different from UIDeviceOrientation?


When will the assertions in the below code fire?

let orien = UIApplication.shared.statusBarOrientation

UIDevice.current.beginGeneratingDeviceOrientationNotifications()

switch UIDevice.current.orientation {
case .portrait: assert(orien == .portrait)
case .portraitUpsideDown: assert(orien == .portraitUpsideDown)
case .landscapeLeft: assert(orien == .landscapeRight)
case .landscapeRight: assert(orien == .landscapeLeft)
default: return
}

Solution

  • When your app doesn't support all four orientations. UIDeviceOrientation reflects the orientation of the device independent of whether your app supports that orientation. statusBarOrientation will always be one of the orientations you support.

    So, for example, if your device is in portrait upside down, but your app doesn't support that orientation, UIDeviceOrientation will be portraitUpsideDown, but statusBarOrientation will be landscapeLeft (if that was the orientation the device was in prior to portrait upside down).

    Note that rotation lock on the iPhone won't make the assertions above fire — both statusBarOrientation and UIDeviceOrientation are reported as portrait, independent of the actual orientation, but they're consistent with each other.