How to programmatically determine iPhone interface orientation?
Asked Answered
S

6

53

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?

Simonesimoneau answered 11/3, 2009 at 14:28 Comment(0)
F
92

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;
Fao answered 11/3, 2009 at 14:40 Comment(3)
if i need to develope iphone app with all interface orientation than what shoul i do..? Is there any doc file or blog tutorial which in your mind please tell me..Artemisa
"Do not use this property (UIViewController.interfaceOrientation) for informing layout decisions. Instead, use the statusBarOrientation property, described in UIApplication Class Reference." says AppleRate
Deprecated since iOS 8Sruti
B
95

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.

Balkin answered 9/10, 2010 at 18:6 Comment(8)
it is not UIInterfaceOrientation, it is - UIDeviceOrientation, that's what micco already mentioned in his post.Ol
You are right. UIDeviceOrientation is not always UIInterfaceOrientation.Balkin
[[UIDevice currentDevice] orientation] does not work if the user has locked his device orientation from the Springboard. orientation is always the last value, and there are no notifications for changes.Fiasco
I tried to use UIDeviceOrientation but was facing problems when the device was placed on table facing up.Using UIInterfaceOrientation solved the issue +1... ThanksDillard
[[UIApplication sharedApplication] statusBarOrientation] work well for me. You can use UIDeviceOrientationIsPortrait(orientation) and UIDeviceOrientationIsLandscape(orientation) with any of those solutions if you don't care about left, rigt or up, down.Palawan
I had been using tons of workarounds before reading this answer due to my classes not all being UIViews...this works epicly well! I'd give a million upvotes to this if I could...thanks a ton! @BalkinSelfindulgent
yes, UIInterfaceOrientation orientation is the best way to check state of orientationTierell
So 1. and 2. is deprecated and 3. doesn't return the real interface orientation... But why is 2. deprecated on the Apple developer website but not in the iOS 10.1 SDK?Sruti
F
92

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;
Fao answered 11/3, 2009 at 14:40 Comment(3)
if i need to develope iphone app with all interface orientation than what shoul i do..? Is there any doc file or blog tutorial which in your mind please tell me..Artemisa
"Do not use this property (UIViewController.interfaceOrientation) for informing layout decisions. Instead, use the statusBarOrientation property, described in UIApplication Class Reference." says AppleRate
Deprecated since iOS 8Sruti
I
14

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)
Issiah answered 23/4, 2012 at 7:16 Comment(0)
D
11

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];
Dykes answered 27/10, 2011 at 5:9 Comment(0)
E
5

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.

Evince answered 11/3, 2009 at 14:48 Comment(1)
I've used the status bar orientation as a working reference point on the iPad. Bound's checking didn't work for me.Sexdecillion
C
1

Here's a codesnippet you might find useful:

UIInterfaceOrientation  orientation = [UIDevice currentDevice].orientation;
NSLog( @" ORIENTATION: %@", UIInterfaceOrientationIsLandscape( orientation ) ? @"LANDSCAPE" : @"PORTRAIT");
Cow answered 23/5, 2013 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.