Prevent autorotate for one view controller?

匆匆过客 提交于 2019-12-18 11:25:44

问题


My app can autorotate but I need one of the views to only show in portrait mode and don't know how to achieve this.

I tried this (among other things) but the view in question still rotates:

//  ViewController.m

-(BOOL)shouldAutorotate
{            
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Can someone kindly point out what I'm doing wrong? Thanks.

-edit-

It's for iOS 6.1


回答1:


When a UINavigationController is involved, create a category on the UINavigationController and override supportedInterfaceOrientations.

 #import "UINavigationController+Orientation.h"

 @implementation UINavigationController (Orientation)

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

-(BOOL)shouldAutorotate
 {
    return YES;
 }

@end  

Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate.

How to create a category
1. Add a new file (Objective c- category under cocoa touch)
2. Category : Orientation on UINavigationController
3. Add the above code to UINavigationController+Orientation.m




回答2:


Swift 3 version the accepted answer:

extension UINavigationController {

    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        // Change `.portrait` to whatever your default is throughout your app
        return topViewController?.supportedInterfaceOrientations ?? .portrait
    }

    open override var shouldAutorotate: Bool {
        return true
    }
}



回答3:


As per the documentation.

A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations.

So we need to override shouldAutorotate and supportedInterfaceOrientation to target view controllers.

Typically, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen.

This will work if you have very simple configuration like your target view controller is the rootViewController of window or being presented covering whole screen.

In case when configuration of target view controller is complex like embedded in some other container view controllers.

child view controllers use the portion of the window provided for them by their parent view controller and no longer participate directly in decisions about what rotations are supported.

So may be default implementation of these container view controllers not asking there children for there supportedInterfaceOrientation preference.

So to allow our target child view controller to specify there supportedIntefaceOrientation we need to tell there container view controller to do so.

You can also check my previous answer here.

and Understanding UIViewController rotation when embed in Container View Controllers.



来源:https://stackoverflow.com/questions/15004271/prevent-autorotate-for-one-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!