简体   繁体   中英

Unable to lock device orientation on view controller with navigation controller

I have 9 view controllers in a push navigation stack. I'd like all my view controllers to be locked to portrait but my 4th view controller to support all orientations. I have tried a number of methods, but currently have globally supported device orientations set to Portrait and have added this code to my 4th view controller in order for it to support all orientations.

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);
}

If I try this on a single view controller without the navigation controller it works fine. I have also attempted to implement these methods on all view controllers, setting the global settings to support all orientations and adding the code to support portrait orientations but to no avail.

It is indeed possible, and I did it in one of my current projects. First of all you need to have all orientation options that you want to support ticked in the Project general tab. Secondly you create a subclass of UINavigationController which will become your navigation controller, so for example if you have a Storyboard with a navigation controller you have to make it of your custom navigation controller from the Identity Inspector tab in Interface Builder.

You obviously need to override some methods in such subclass, which are:

- (BOOL)shouldAutorotate {
    return YES;
}

This tells the navigation controller that it needs to allow view controllers to rotate. After that, override the following method, reading carefully the implementation that follows:

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

This takes the supported interface orientations from its topViewController (the currently visible one).

So you need to override the same method an all your view controllers like following:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

which will lock itself on portrait and you want to return:

UIInterfaceOrientationMaskSomethingDifferentFromPortrait

which in my case was

UIInterfaceOrientationMaskAllButUpsideDown

Hope it helps.

Try using this in all your view controllers to fix them to portrait,

  • (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

     return UIInterfaceOrientationMaskPortrait; 

    }

for the 4th viewController add

  • (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

     return UIInterfaceOrientationMaskAllButUpsideDown; 

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM