iPad Device Orientation is Faceup or Facedown & Interface Orientation is Landscape or Portrait... How to Know this programmatically?

Is there any way to find out iPad is in faceup position & user interface is in landscape or protrait mode....?


If this is possible how can I write conditional code for that orientation?

Replies

you can use UIDevice.current.orientation :

if UIDevice.current.orientation == .faceDown { }


This is also used to test all the values: face up or down, portrait or landscape…;

Note :

The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application’s user interface. See

UIDeviceOrientation
for descriptions of the possible values.

The value of this property always returns 0 unless orientation notifications have been enabled by calling

beginGeneratingDeviceOrientationNotifications()
.


However, I tested on simulator, without enabling notifications, and it works.

-----------------------

There was statusBarOrientation, which did not require to enable notifications ; it is now marked deprecated, but still working


            let orientation = UIApplication.shared.statusBarOrientation


possible values are :

unknown

portrait

portraitUpsideDown

landscapeLeft

landscapeRight


You can also test with

if UIApplication.shared.statusBarOrientation.isLandscape { }


It is now recommended to use UITraitCollection, see details here

https://stackoverflow.com/questions/24071453/rotation-methods-deprecated-equivalent-of-didrotatefrominterfaceorientation/43345307#43345307