How to programmatically determine iPhone interface orientation?

IphoneCocoa Touch

Iphone Problem Overview


The possible values for UIDevice.orientation include UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown. While it might be useful to know that the device is flat, this doesn't tell us whether it's flat displaying an interface oriented in portrait or landscape mode. Is there a way to find the current orientation of the GUI in cases where the device is returning ambiguous information? I suppose I could track orientation change events to remember the last portrait/landscape orientation or check the main UIView's bounds height and width, but that seems kludgey. Is there a property on the device or UIView that I'm missing?

Iphone Solutions


Solution 1 - Iphone

You can get orientation by 3 ways:

  1. UIInterfaceOrientation orientation = self.interfaceOrientation; returns UIInterfaceOrientation, current orientation of the interface. It is a property in UIViewController, you can access to this one only in UIViewController classes.

  2. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; returns UIInterfaceOrientation, current orientation of the application's status bar. You can access to that property in any point of your application. My experience shows that this is the most effective way to retrieve real interface orientation.

  3. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; returns UIDeviceOrientation, device orientation. You can access to that property in any point of your application. But note that UIDeviceOrientation is not always UIInterfaceOrientation. For example, when your device is on a plain table you can receive unexpected value.

Solution 2 - Iphone

In a viewcontroller you can simply use the code:

UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;

The UIInterfaceOrientation is an enum:

typedef enum {
  UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
  UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeLeft,
  UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeRight
} UIInterfaceOrientation;

Solution 3 - Iphone

If you just care that the device is landscape or portrait, there's some nice convenience methods on the viewcontroller:

UIDeviceOrientationIsLandscape(self.interfaceOrientation)
UIDeviceOrientationIsPortrait(self.interfaceOrientation)

Solution 4 - Iphone

Status bar orientation (statusBarOrientation) always returns the interface orientation even if the status bar is hidden.

You can use the status bar orientation without a view controller. It gives you the current view orientation, not the device orientation.

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

Solution 5 - Iphone

I've seen the same situation. Specifically in this flow:

  • Controller 1 loads in say.. Portrait.
  • Push another controller onto the stack
  • While in the 2nd controller, rotate the device orientation to Landscape.
  • Lay the device flat on a table and then pop back to the original controller.

At this point, any orientation checks I've seen return an invalid orientation of 5, so it's not directly possible to determine if a landscape or portrait layout should be used. (I'm doing custom layout positioning on a per-orientation basis, so this is significant information)

In my case, a bounds width check on the view is used to determine the true state of things, but I'd love to know if others have addressed differently.

Solution 6 - Iphone

Here's a codesnippet you might find useful:

UIInterfaceOrientation  orientation = [UIDevice currentDevice].orientation;
NSLog( @" ORIENTATION: %@", UIInterfaceOrientationIsLandscape( orientation ) ? @"LANDSCAPE" : @"PORTRAIT");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmiccoView Question on Stackoverflow
Solution 1 - IphoneberylliumView Answer on Stackoverflow
Solution 2 - IphonegeorgView Answer on Stackoverflow
Solution 3 - IphoneChris O'SullivanView Answer on Stackoverflow
Solution 4 - IphoneTibidaboView Answer on Stackoverflow
Solution 5 - IphonejberryView Answer on Stackoverflow
Solution 6 - IphonecleverbitView Answer on Stackoverflow