简体   繁体   中英

Force UIViewController orientation

I did some research but cannot seem to find the answer to have my rootViewController of my navigationController be correct on start up. My original question was here: launch orientation of iPad is incorrect for landscape orientation (upside down) .

In my info.plist, I have it set to support both landscape orientations. If I change my rootViewController to be:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//    return UIInterfaceOrientationIsLandscape(interfaceOrientation); // original that does not work
    return YES;
}

then my app starts in the right orientation. However, I do not want to support portrait modes. I only want to support landscape modes. I thought I could force the orientation and prevent it from switching to portrait modes by doing something like this:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        UIDeviceOrientation orientation = UIInterfaceOrientationLandscapeLeft;
        [UIApplication sharedApplication].statusBarOrientation = orientation;
    }
    else {

    }

But this does not prevent the app from being rotated to portrait mode. Is it possible to force the orientation? Is there something else I need to do in order for the startup orientation to be correct (landscape mode only)? Thanks!

I just created a sample project, set my Supported interface orientations (iPad) to Landscape Left and Landscape Right (in info.plist).

I then used:

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

and it works fine. It's forced to landscape not matter how i rotate it.

Do you have any other view controllers visible that might be returning YES to all orientations? This could confuse it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM