I'm trying to find out if the device is in portrait or landscape mode. My code works quite well if the device is not facing up. If it does face up (and orientation == 5), it won't distinguish between portrait and landscape. Is there anyway to determine the "orientation" in terms of landscape / portrait if the UIDeviceOrientation is FaceUp?
My code:
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
NSLog(@"orientation: %d", interfaceOrientation);
if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) {
NSLog(@"LANDSCAPE!!!");
}
if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) {
NSLog(@"PORTRAIT!!!");
}
You should not confuse
UIDeviceOrientation
andUIInterfaceOrientation
, they are different but related as shown by their declarationUIDeviceOrientation
tells you what the orientation of the device is.UIInterfaceOrientation
tells you what the orientation of your interface is, and is used byUIViewController
.UIInterfaceOrientation
will clearly be either portrait or landscape, whereasUIDeviceOrientation
can have ambiguous values (UIDeviceOrientationFaceUp
,UIDeviceOrientationFaceDown
,UIDeviceOrientationUnknown
).In any case you should not attempt to determine the orientation of a UIViewController with
[[UIDevice currentDevice] orientation]
, as no matter what the device orientation is theUIViewController interfaceOrientation
property can be different (for example if your app does not rotate to landscape at all[[UIDevice currentDevice] orientation]
can beUIDeviceOrientationLandscapeLeft
whileviewController.interfaceOrientation
can beUIInterfaceOrientationPortrait
).Update: As of iOS 8.0,
[UIViewController interfaceOrientation]
is deprecated. An alternative offered here is[[UIApplication sharedApplication] statusBarOrientation]
. This also returnsUIInterfaceOrientation
.