开发者

Interface Orientation won't change to Landscape on App Launch

开发者 https://www.devze.com 2023-03-07 23:27 出处:网络
i stuck on a problem that drives me crazy! I have an iPad application starting with a TabBarViewController. Every Tab has it\'s own ViewController and nib-file.

i stuck on a problem that drives me crazy! I have an iPad application starting with a TabBarViewController. Every Tab has it's own ViewController and nib-file.

To make interface orientation possible I added all orientations to my info.plist and subclassing my TabBarController to set:

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}

Every View is autoresized and formatted well for displaying the right Orientation if I rotate the Interface. If I test it in the simulator, everything looks fine and I can rotate between the Orientations.

Now the point that drives me crazy:

If I launch the App in Portrait Mode, all works fine.. but if I launch in Landscape, I get an error and m开发者_开发知识库y Interface orientation seems still to be Portrait, while the Simulator is in Landscape Mode!!

The Error:

2011-05-24 21:50:15.011 Project[57995:207] Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.

I checked for Orientation like this:

    -(void)viewWillAppear:(BOOL)animated{

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];



    if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
        NSLog(@"Orientation: Landscape");
    }
    else{
        NSLog(@"Orientation: Portrait");
    }

}

The Log says it is in "Landscape" Mode if I launch in Landscape, but if I change tab to another it looks terrible because the View is displayed in Portrait mode instead. On change back to the start-view where i asked for Orientation… the log displays "Portrait"… but the Simulator is still Landscape!

I can't figure out why the Orientation is Portrait on start,… even if I start in Landscape…

Any idea how to solve this problem?


Ok. So the documentation says viewWillAppear: is called prior to all animations. And when your app starts, its initial orientation is Portrait. Based on what your orientation is, it then rotates to that orientation. Since it animates to the orientation off screen, this must be called after viewWillAppear:/ So when viewWillAppear: is called, its still in Portrait. I tested this myself.


I solved the problem!

Simply used this in viewWillAppear method of all my Views, to make it work when started in Landscape mode:

if(([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) || ([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationLandscapeRight)){        
    //Landscape View
}
else{
    //Portrait View
}

Works fine for me!

0

精彩评论

暂无评论...
验证码 换一张
取 消