Search code examples
iosobjective-cios9uiinterfaceorientation

supportedInterfaceOrientationsForWindow returns UIInterfaceOrientationMaskPortrait but my viewcontroller show in landscape


I am handling all the orientations in the appdelegate.m file. supportedInterfaceOrientationsForWindow returns UIInterfaceOrientationMaskLandscape for some of my view controllers and UIInterfaceOrientationMaskPortrait for one viewcontroller.

When I move from a landscape viewcontroller to the viewcontroller I want to portrait supportedInterfaceOrientationsForWindow returns UIInterfaceOrientationMaskPortrait but my view controller appears in landscape.

Please help me I've been stuck for a long time. Testing on iPhone 6, iOS 9.


Solution

  • Let's say view A is one of the landscape views of your app and B is the intended portrait view. Now, when the device is initially held in portrait on A and B is then accessed, the view doesn't change the orientation to portrait (since the device was already in portrait).

    I'd suggest 'tell' system that B is initially in landscape while preparing the segue and then in your viewDidLoad for B's view controller, change the orientation to portrait.

    This way, no matter what the previous view's orientation, B will always open in portrait when it loads.

    Use the following code to change the orientation. In case you are using segue use this code in

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
    

    The orientation here should be opposite of the desired orientation in the next view controller.