How to get iPhones current orientation?

Knodel picture Knodel · Mar 25, 2010 · Viewed 48.2k times · Source

Is there a special method to get iPhones orientation? I don't need it in degrees or radians, I want it to return an UIInterfaceOrientation object. I just need it for an if-else construction like

if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}  
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}

Thanks in advance!

Answer

Satyajit picture Satyajit · Mar 25, 2010

This is most likely what you want:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

You can then use system macros like:

if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{

}

If you want the device orientation use:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

This includes enumerations like UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown