Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong value for statusBarOrientation on viewWillAppear

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.

like image 816
miguel.rodelas Avatar asked Dec 07 '22 14:12

miguel.rodelas


2 Answers

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");
    }
like image 53
Mina Nabil Avatar answered Jan 12 '23 14:01

Mina Nabil


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.

like image 22
Sani Avatar answered Jan 12 '23 13:01

Sani