Search code examples
iosobjective-cuiviewcontrolleruiinterfaceorientation

Portrait orientation in all view controllers except in modal view controller


I'm presenting a modal view with the code:

 [self presentViewController:movieViewController animated:YES completion:^{
     // completed
 }];

Inside movieViewController, the controller is dismissed with:

[self dismissViewControllerAnimated:YES completion:^{
    // back to previous view controller
}];

At the moment, all my view controllers can be viewed in portrait and the two landscape orientations.

How would I restrict all view controllers to portrait EXCEPT the modal view controller? So the modal view controller can be seen in the three orientations, and everything else in one.


Solution

  • In appDelegate:

    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    

    special UINavigationController subclass (if you using navigation controller) with methods:

    - (NSUInteger)supportedInterfaceOrientations {
        if (self.topViewController.presentedViewController) {
            return self.topViewController.presentedViewController.supportedInterfaceOrientations;
        }
        return self.topViewController.supportedInterfaceOrientations;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return self.topViewController.preferredInterfaceOrientationForPresentation;
    }
    

    each view controller should return it's own supported orientations and preferred presentation orientation:

    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return UIInterfaceOrientationPortrait;
    }
    

    my app works in portrait, but video player opens as modal vc with:

     - (NSUInteger)supportedInterfaceOrientations {
         return UIInterfaceOrientationMaskAllButUpsideDown;
     }
    
     - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
         return UIInterfaceOrientationLandscapeLeft;
     }
    

    It works perfect for me, hope it helps!