4

I need to change the image background of a View depending on the orientation. For this, I am using the statusBarOrientation approach in viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
        NSLog(@"PORTRAIT");
    } else if (UIInterfaceOrientationIsLandscape(currentOrientation)) {
        NSLog(@"LANDSCAPE");
    }   
}

The problem is that the console is always showing PORTRAIT, even when the iPad is held in landscape mode. The same code in viewDidAppear works correctly, but there is too late and the user can see the change of images. That makes me think that the correct state of statusBarOrientation is still not available in viewWillAppear, but I have read in some other questions that this code should work there.

MRD
  • 7,146
  • 1
  • 21
  • 21
  • What's even weirder is that in viewWillTransitionToSize: you can query the orientation, and if you rotate the phone twice (back to its viewDidLoad position), statusBarOrientation'll give you the correct answer. Even though it gave a different answer in viewDidLoad, and now the device is in the same orientation as then! – Graham Perks Oct 30 '15 at 15:23

3 Answers3

12

Try

int type = [[UIDevice currentDevice] orientation];
    if (type == 1) {
        NSLog(@"portrait default");
    }else if(type ==2){
        NSLog(@"portrait upside");
    }else if(type ==3){
        NSLog(@"Landscape right");
    }else if(type ==4){
        NSLog(@"Landscape left");
    }
Mina Nabil
  • 676
  • 6
  • 19
  • 1
    Ok, interesting... I read everywhere that the statusbar orientation approach is more reliable that the UIDevice one, but this is working like a charm! Thanks! – MRD Jun 11 '12 at 14:36
  • 1
    i searched everywhere...interfaceOrientation, statusBar all were returning wrong orientation , Then This solution worked +1.. – iMeMyself Oct 15 '13 at 09:11
3

You shouldn't be using the statusBarOrientation to determine the current orientation of the application. According to Apple's doc: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html

The value of this property is a constant that indicates an orientation of the receiver's status bar. See UIInterfaceOrientation for details. Setting this property rotates the status bar to the specified orientation without animating the transition. If your application has rotatable window content, however, you should not arbitrarily set status-bar orientation using this method. The status-bar orientation set by this method does not change if the device changes orientation.

Try using the interfaceOrientation property of a UIViewController to get the orientation of the current application.

Sani
  • 1,283
  • 11
  • 30
  • 1
    I used this approach because it's recommended in several answers here in Stackoverflow to get the orientation. With the [[UIDevice currentDevice] orientation] approach it worked. Reading the property interfaceOrientation worked as well. – MRD Jun 11 '12 at 14:46
  • The link to the Apple documentation doesn't say that you shouldn't be using it to determine current orientation. Can you give us a reference to a source that says why you shouldn't? In fact, Apple's sample code uses it: https://developer.apple.com/library/content/samplecode/AVCam/Listings/Swift_AVCam_CameraViewController_swift.html#//apple_ref/doc/uid/DTS40010112-Swift_AVCam_CameraViewController_swift-DontLinkElementID_15 Cmd-F for statusbarOrientation. In fact, UIDevice orientation warns that it could be different from your UI's orientation, so judging by the docs, one should avoid it. – Kartick Vaddadi Dec 21 '16 at 04:55
  • This gives issues on SOME devices. Thanks apple. – Warpzit Feb 10 '17 at 12:34
  • `interfaceOrientation` is deprecated. – Jonny Jan 30 '18 at 10:25
0

Here is a useful code snippet for logging the device's interfaceOrientation:

NSArray* orientations = @[ @"nothing", @"UIInterfaceOrientationPortrait", @"UIInterfaceOrientationPortraitUpsideDown", @"UIInterfaceOrientationLandscapeLeft", @"UIInterfaceOrientationLandscapeRight”];
NSLog(@"Current orientation := %@", orientations[self.interfaceOrientation]);

Hope this helps someone out!

Glavid
  • 1,071
  • 9
  • 19