Determine UIInterfaceOrientation on iPad

52,447

Solution 1

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]?

Solution 2

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

Solution 3

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.

Solution 4

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if ((orientation == UIInterfaceOrientationLandscapeLeft)
||  (orientation == UIInterfaceOrientationLandscapeRight) )
{
    //Landscape
}
else
{
    //Portrait
}

Solution 5

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.

Share:
52,447
Daddy
Author by

Daddy

i like programming!

Updated on February 15, 2020

Comments

  • Daddy
    Daddy over 4 years

    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!

  • Daddy
    Daddy about 14 years
    Your answer helps tremendously. Now all I have to test for is if isPortrait is true, instead of trying to add orientation code into the viewController rotation methods
  • M. Ryan
    M. Ryan about 14 years
    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.
  • Joo Park
    Joo Park about 14 years
    i believe when the app first start up, the default orientation is portrait. That might explain why [self interfaceOrientation] always returns 1.
  • Tirth
    Tirth almost 14 years
    @zoul, isPortrait bool value returning me "YES" in simulator. I have not iPad device, then how i detecting current device orientation in viewDidLoad?
  • Artilheiro
    Artilheiro almost 14 years
    this does not work on detailViewController inside a split view... isPortrait is always YES. Any ideas?
  • Daredzik
    Daredzik over 13 years
    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.
  • slycrel
    slycrel over 13 years
    @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.
  • Sam Stewart
    Sam Stewart over 13 years
    Awesome, really reliable on the sim. Thanks!
  • MusiGenesis
    MusiGenesis about 13 years
    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.
  • zmippie
    zmippie about 13 years
    @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).
  • MusiGenesis
    MusiGenesis about 13 years
    @zmippie: what isn't true, that self.interfaceOrientation is NULL in initial load or that the statusBarOrientation is never NULL? Those are both true for me.
  • Admin
    Admin almost 13 years
    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.
  • Sam
    Sam over 12 years
    I don't like using UIDeviceOrientation for orientations since when it is face up / face down it is more or less useless if you're trying to determine if your app is in landscape or portrait. This initially caused a bug in my code until I switched to using [UIApplication sharedApplication].statusBarOrientation. However, I wasn't trying to get the orientation during startup... Just my thoughts on this.
  • AP inc.
    AP inc. over 12 years
    use [[UIDevice currentDevice] orientation] in viewDidLoad
  • ToddH
    ToddH over 11 years
    If you're going to inspect self.interfaceOrientation you need to use UIInterfaceOrientationIsPortrait() instead of UIDeviceOrientationIsPortrait().
  • KDeogharkar
    KDeogharkar about 11 years
    this is very useful to me because first time run UIInterfaceOrientation orientation = (UIInterfaceOrientation)[UIDevice currentDevice].orientation; always return 0 . even if it is portrait.
  • barfoon
    barfoon almost 10 years
    interfaceOrientation first deprecated in iOS 8.0
  • Chris Conover
    Chris Conover over 8 years
    aka UIInterfaceOrientationIsLandscape()
  • Jerry Chen
    Jerry Chen almost 8 years
    Unfortunately, this quite good solution is deprecated in iOS 9
  • rockdaswift
    rockdaswift over 5 years
    Sometimes you need the orientation of the device to rotate the camera input