Setting View orientation to portrait is ignored

244 views Asked by At

I'm attempting to set a view's orientation based on if the device is running on an iPhone or an iPad. I have a supportedInterfaceOrientations method, which I call from viewDidLoad:

[self supportedInterfaceOrientations];

In the supportedInterfaceOrientations method, I check if the device is an iPhone. If it is, the view should only be set to a portrait orientation. Otherwise, all orientations should be supported:

- (NSUInteger)supportedInterfaceOrientations
{
    //If the device is an iPhone, we're going to make this page portrait-only
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskPortrait;

    }

    //If it's an iPad, we'll support all orientations.
    else {
        return  UIInterfaceOrientationMaskAll;
    }
}

However, when I run the app on an iPhone, the view does not stay locked in portrait mode, but will rotate based based on the phone's orientation. Does anyone have an idea of what I'm doing wrong? Thank you!

EDIT I forgot to mention that this particular view is part of a Navigation Controller, which I just realized is probably the cause of the problem.

EDIT2 The xib for this particular view is also set to 'portrait' in the 'Simulated Metrics' section. Additionally, below is the code where I build the view controller and present the view:

self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
self.navigationController.delegate = self;
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
1

There are 1 answers

0
Vicky On

Use:

- (BOOL)shouldAutorotate {
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    //If the device is an iPhone, we're going to make this page portrait-only
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskPortrait;
    }
    //If it's an iPad, we'll support all orientations.
    else {
        return  UIInterfaceOrientationMaskAll;
    }
}