Force portrait mode

2019-03-03 18:33发布

问题:

Alright, since no one answered my previous question, I have come to believe that there may be no easy way to do this. But I am optimistic. Here's my issue:

In my app, I am switching from ViewControllerOne to ViewControllerTwo by using a regular UIButton. ViewControllerOne is always in landscape mode. ViewControllerTwo is supposed to be always in portrait mode. However, when I push the button in my landscape ViewControllerOne, ViewControllerTwo is also in landscape mode, although I want it to switch to portrait mode regardless of how the device is rotated by the user when the button is pressed.

I added the following code to my AppDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations;
}

And I added this to my ViewController that's supposed to be in portrait mode only:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;

}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

}

-(void)viewWillAppear:(BOOL)animated
{
    UIApplication* application = [UIApplication sharedApplication];
    application.statusBarOrientation = UIInterfaceOrientationPortrait;
}

Is there a way to tell the viewController to go into portrait mode, even if the previous view was landscape? Maybe I could create a custom segue that would force the view to be in portrait mode when the button is pushed? What would be the best/official solution here?

回答1:

Based on my understanding of the question I assume that you want the 2nd view controller to be portrait and the first view controller to be landscape.

For the second viewcontroller, add this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

For the first view controller:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscape);

}


回答2:

Check the question How to handle different orientations in iOS 6. See the answer there for a project example of exactly what you need.

Basically you need to embed a custom navigation controller in your viewcontroller (the one you want to rotate). Add the following method in this custom navigation controller (it is for landscape orientation but you can replace it for portrait)

- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}

and add to your view controller that should rotate:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

Be sure Portrait, Landscape Right and Landscape Left orientations are enabled in your project. Then, if you want to block some orientations for a particular view:

– application:supportedInterfaceOrientationsForWindow: