Portrait orientation in all view controllers except in modal view controller
Asked Answered
H

1

6

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.

Holdfast answered 26/2, 2013 at 20:27 Comment(1)
are you going to use iOS 6 or 5?Rochus
R
21

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!

Rochus answered 27/2, 2013 at 1:2 Comment(2)
i had to force landscape with UIInterfaceOrientationMaskLandscapeLeft in my video player's supportedInterfaceOrientations to get it working. otherwise i would get crashes. but video is meant to be watched landscape, no no probs.Holdfast
No, you need to return all supported orientations as mask and return one of landscape orientation as preferred for presentation.Rochus

© 2022 - 2024 — McMap. All rights reserved.