When is statusBarOrientation different from UIDeviceOrientation?

455 views Asked by At

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

There are 1 answers

0
Kartick Vaddadi On BEST ANSWER

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.