35

I don't need to specify the orientation in this case, I just need to detect it, but I'm having trouble. I have conditional code that should only work in portrait, and if the device is in landscape I need to do something else. Since the deviceOrientation is not necessarily the same as the interfaceOrientation, I can't come up with a way to test for portrait mode.

Most tutorials I find on Google are ways to force landscape or do some sort of rotation. The only thing I want to do is just determine what the orientation is. Here is my code, which is not working:

-(void)viewDidLoad {
    [super viewDidLoad];
    //currentOrientation is declared as UIInterfaceOrientation currentOrientation
    currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
NSLog(@"%@",currentOrientation);  // == NULL
}

I need to determine the value of the interfaceOrientation and program conditionally. Thanks for your help!

10 Answers 10

77

Are you aware of the interfaceOrientation property of the UIViewController class?

- (void) viewDidLoad {
    [super viewDidLoad];
    BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
    // now do whatever you need
}

Or are you after [[UIDevice currentDevice] orientation]?

12
  • 2
    Be aware that this doesn't always seem to work. Particularly if your controller is the first in your application. In my simulator tests [self interfaceOrientation] always returns as 1. Maybe it's different on the actual device.. at the very least.. hard to test.
    – M. Ryan
    Apr 15, 2010 at 2:08
  • 4
    I just ran into this problem myself. The ultimate reason is that self.interfaceOrientation is NULL when the view initially loads, so that both UIDeviceOrientationIsPortrait and UIDeviceOrientationIsLandscape return NO. Using [UIApplication sharedApplication].statusBarOrientation in the answer below always works correctly, since that orientation is never NULL. Jun 9, 2011 at 15:20
  • 2
    @MusiGenesis: not true, as others have mentioned: it's often (in my case, always) 1 (portrait) when the app launches. There's no difference between interfaceOrientation in your (first) view controller or [UIApplication sharedApplication] either. It is really hard, if not, impossible to know the UI orientation at launch (device orientation isn't telling you anything when it's face up or down).
    – zmippie
    Jun 11, 2011 at 8:33
  • 1
    use [[UIDevice currentDevice] orientation] in viewDidLoad
    – AP inc.
    Mar 2, 2012 at 17:08
  • 3
    interfaceOrientation first deprecated in iOS 8.0
    – barfoon
    Sep 3, 2014 at 15:58
41

Especially at launch I have found the following to be always accurate for the UI, regardless of what the UIDevice says the orientation is.

[UIApplication sharedApplication].statusBarOrientation
5
  • Um, isn't that basically the exact same thing in the original question? I've found in my tests that it always returns 1 when the app inits, and is therefore pretty useless.
    – Matt Rix
    Nov 28, 2010 at 6:27
  • @Matt, I think you're correct. No idea why I posted this back in may, it does indeed look like it's the same code as the original. I may have misunderstood the question, as I've used the above when the device orientation is unknown, face up or face down.
    – slycrel
    Nov 30, 2010 at 18:47
  • Awesome, really reliable on the sim. Thanks! Jan 4, 2011 at 19:06
  • this is very useful to me because first time run UIInterfaceOrientation orientation = (UIInterfaceOrientation)[UIDevice currentDevice].orientation; always return 0 . even if it is portrait. Apr 22, 2013 at 5:20
  • Unfortunately, this quite good solution is deprecated in iOS 9
    – Jerry Chen
    Aug 12, 2016 at 7:21
10

self.interfaceOrientation is unreliable in certain situations. For example, re-arranging tabs in a tabbar application returns incorrect value.

However [UIApplication sharedApplication].statusBarOrientation is always reliable. You saved me a lot of time slycrel. Thank you.

1
  • 2
    It may be worth noting that it is not safe to assume statusBarOrientation is valid early on in execution. I appears as though this value is updated after didFinishLaunchingWithOptions exits. That is using iOS simulator 4.3.
    – doc taco
    Aug 29, 2011 at 0:49
4
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if ((orientation == UIInterfaceOrientationLandscapeLeft)
||  (orientation == UIInterfaceOrientationLandscapeRight) )
{
    //Landscape
}
else
{
    //Portrait
}
1
  • 1
    aka UIInterfaceOrientationIsLandscape() Feb 1, 2016 at 22:27
1

I know it is a very old post. How ever I would like to add a point to say it is better to check status bar orientation is better. When ever you call self.interfaceorientation it is calling shouldRotateToOrientation every time. If you have written some code in that method it will be executed. So be cautious!.

1
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation; 

    if(deviceOrientation == UIDeviceOrientationFaceUp || deviceOrientation == UIDeviceOrientationFaceDown){
        if(debug){
            NSLog(@"Nothing to change because it is gone to Flat");
        }
        return;
    }
    if(deviceOrientation !=statusBarOrientation){
        if(debug){
            NSLog(@"\nApple has a bug?:\n UIDeviceOrientation : %d, UIInterfaceOrientation: %d",deviceOrientation, statusBarOrientation  );
        }
    }

You won't believe me until you will see at the console the second output! Some situations - and they exists! - is displayed the last NSLog content!

Than you have to do some workarounds to go on that way, where iOS has no bug, good luck for everyone!

Ah that ... forum moderator maybe will delete this post too, because this doesn't meant to be and answer in his opinion!

I hope it helps for somebody once, it happens on iphone too...(there I got)

1

Mix it up a little:

BOOL isLandscape = self.view.frame.size.width > self.view.frame.size.height;

(edit) Obviously the previous answers are the correct way to do this and this solution would fail in a situation where view controllers are not full-screen.

1
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{</br>
   if (UIDeviceOrientationIsLandscape(toInterfaceOrientation)) {</br>
        some instructions;
   } else { 
        some instructions;
   }
}

This is a snippet from one of my programs. You could of course use the if statement in your ViewDidLoad notification as well.

1

I already voted up the answer by @slycrel, but I would like to take the time to write this, and point some things out that seems to be lost in this old question, and lots of other questions on the subject.

It's true that Apple does not really want us to update most of our UI based on orientation changes, but it is still totally possible and sometimes necessary on a case by case scenario, and it will be that way until Apple improves their new(ish) APIs (e.g. viewWillTransitionToFrame: would be way more useful than viewWillTransitionToSize:. Just sayin')

Why I voted up the answer by @slycrel is related to what you need to keep in mind as the logical difference between UIDeviceOrientation and UIInterfaceOrientation.

Tthe status bar is what denotes an application's currently known UIInterfaceOrientation. All this stuff about FaceUp, FaceDown is only related to a device's orientation, not necessarily your application's. An application does not support device orientations anyway. Really, UIDeviceOrientation can be ignored completely if all you have to do is make sure you layout and animate things appropriately in your interface, which is 99% of an application developer's use cases. This is currently achieved with the status bar's UIInterfaceOrientation from @slycrel's answer:

[UIApplication sharedApplication].statusBarOrientation

It should be noted, the readwrite version of this property is deprecated, the readonly version is not.

Take this example:

  • I have an application that supports ALL interfaces orientations, and a root view controller that supports them as well.
  • Now, I am presenting a UIViewController that will result in the status bar orientation to become landscape.
  • Which landscape orientation (left or right) it goes to is based on what is returned by preferredInterfaceOrientationForPresentation for that view controller, what the current device orientation is, and what interface orientations the view controller supports (see next point).
  • The status bar will go to landscape, regardless of what the current device orientation is, because this view controller only supports landscape based on what is returned by supportedInterfaceOrientations. Lets say we support both landscape left and right with UIInterfaceOrientationMaskLandscape.
  • I also want to conditionally animate this view controller into position with a rotation transform. This will only be necessary when going from portrait or portrait upside down, to landscape left or landscape right. Otherwise it will be a more simple presentation animation without rotation.
  • Then, after some time and device use, I dismiss that view controller.
  • Now I want to conditionally animate this view controller off the screen with another rotation transform. This will only be necessary when going from landscape left or landscape right, to portrait or portrait upside down. Otherwise it will be a more simple dismissal animation without rotation.
  • At this point, the status bar's orientation will become whatever the system decides is appropriate for the combination of your root view controller's preferred interface orientation and supported interface orientations, as well as the device's current UIDeviceOrientation.
  • Since the view controller we are going to supports ALL interface orientations, if your device's orientation is FaceUp or FaceDown, you can not reliably guess the next UIInterfaceOrientation based on UIDeviceOrientation, and you do not have to anyway.
  • So... status bar orientation to the rescue!

The previous example is possible, because the status bar orientation is not updated when a view controller transition is about to start (the system asks a transition delegate for an animator, etc.). Then it is updated when the transition starts animating (e.g. by the time animationTransition: is called). This way you should have a good comparison just using the initial and current values of the status bar's UIInterfaceOrientation.

Even without using view controller transitions, it should still be safe to update views based on the status bar orientation.

Keep in mind, if you are manually updating the status bar, and if you are not using "View controller-based status bar appearance" in your Info.plist, then your application's logic must be aware when the status bar will and did change orientation. You will probably be looking for a couple NSNotification names for these cases, which are:

  • UIApplicationWillChangeStatusBarOrientationNotification
  • UIApplicationDidChangeStatusBarOrientationNotification

As well as these UIApplicationDelegate methods:

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;
- (UIInterfaceOrientationMask)supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window

And this other helpful UIApplication property:

@property(nonatomic,readonly) NSTimeInterval statusBarOrientationAnimationDuration;
0

As of iOS8, APIs have been deprecated or return unhelpful results such as .FaceUp .FaceDown

This is because Apple does NOT want you to update your UI using orientation, but rather by using size classes, constraints, and proportion (using n% of superview).

Indeed, orientation dependent code might fail to provide good results across the whole range of device and use case (especially multitasking)

1
  • Sometimes you need the orientation of the device to rotate the camera input Jan 8, 2019 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.