Crystal
Crystal

Reputation: 29448

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!

Upvotes: 1

Views: 1804

Answers (1)

bandejapaisa
bandejapaisa

Reputation: 26952

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.

Upvotes: 1

Related Questions