44

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!

Knodel
  • 4,359
  • 8
  • 42
  • 66

5 Answers5

118

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

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
Satyajit
  • 3,839
  • 1
  • 19
  • 14
  • 30
    This is technically wrong, you're getting a UIDeviceOrientation from your method call and storing it in a UIInterfaceOrientation. A UIInterfaceOrientation is one of the 4, and represents what your interface looks like. The UIDeviceOrientation is one of 6 (includes laying flat and laying flat upside down) and tells you the orientation of the device, not necessarily what your interface looks like. Your interface could be in portrait mode but if the device is sitting on a table it would return UIDeviceOrientationFaceUp. – Cory Imdieke Apr 26 '11 at 02:36
  • 12
    @CoryImdieke: Thanks for updating the difference between the device & interface orientation. UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; is the correct API – Senthil Dec 22 '11 at 12:08
  • 2
    This technically is wrong I agree with @Cory but does work quite well. I will post below a method that will not give errors and is the same concept. – FreeAppl3 Apr 16 '12 at 23:54
  • This works well for times when you don't want the interface to change orientation but at the same time you want orientation feedback without having to use the gyro. – Ohmnastrum Nov 18 '13 at 17:16
  • 2
    `statusBarOrientation ` is now deprecated. – Rivera Apr 11 '16 at 15:38
  • deprecated? what to use then? – Rajneesh071 May 16 '16 at 11:27
  • Deprecated since iOS 9. https://developer.apple.com/documentation/uikit/uiapplication/1623026-statusbarorientation – Avi Levin Feb 04 '18 at 22:09
11

As discussed in other answers, you need the interfaceOrientation, not the deviceOrientation.

The easiest way to get to this, is to use the property interfaceOrientation on your UIViewController. (So most often, just: self.interfaceOrientation will do).

Possible values are:

UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft

Remember: Left orientation is entered by turning your device to the right.

Bjinse
  • 1,339
  • 12
  • 25
4

Here is a snippet of code I hade to write because I was getting wierd issues coming back into my root view when the orientation was changed .... I you see just call out the method that should have been called but did seem to be .... this works great no errors

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
        //do something or rather
        [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
        NSLog(@"landscape left");
    }
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
        //do something or rather
        [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
        NSLog(@"landscape right");
    }
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
        //do something or rather
        [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
        NSLog(@"portrait");
    }
}
Antoine
  • 23,526
  • 11
  • 88
  • 94
FreeAppl3
  • 858
  • 1
  • 15
  • 32
1

UIInterfaceOrientation is now deprecated, and UIDeviceOrientation includes UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown so can't be relied on to give you the interface orientation.

The solution is pretty simple though

if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) {
    // Landscape
} else {
    // Portrait
}
trapper
  • 11,716
  • 7
  • 38
  • 82
0

With [UIApplication statusBarOrientation] being deprecated you should now use:

UIWindowScene *activeWindow = (UIWindowScene *)[[[UIApplication sharedApplication] windows] firstObject];

UIInterfaceOrientation orientation = [activeWindow interfaceOrientation] ?: UIInterfaceOrientationPortrait;
Alex Pelletier
  • 4,933
  • 6
  • 34
  • 58