How to disable portrait orientation on UINavigationController rootViewController

浪尽此生 提交于 2019-12-08 03:40:16

问题


I have a class, HomeView, as the rootViewController of a UINavigationController on my iPad application. Only landscape orientation is set in the info.plist file and HomeView does not implement shouldRotateToInterfaceOrientation:, however the view is rotating for both orientations while the simulator is rotating.

How do I only use the landscape orientations in my UIViewController?


回答1:


If you do not implement shouldAutorotateToInterfaceOrientation: the runtime will call the method on the Super class, UIViewController.

Instead you should respond appropriately to the method with the orientations you want to rotate to:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    BOOL shouldAutorotate = NO;

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
        || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {

        shouldAutorotate = YES;
    }

    return shouldAutorotate;
}

Take a look at the UIInterfaceOrientation reference page and the UIViewController's shouldAutorotateToInterfaceOrientation: reference page for more information.




回答2:


You shouldn't remove the shouldAutorotateToInterfaceOrientation method; that will not stop the interface from rotating, it will only not execute any code that you could have in that method. You should instead try something like this (assuming that you don't ever want the interface to rotate):

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


来源:https://stackoverflow.com/questions/8343921/how-to-disable-portrait-orientation-on-uinavigationcontroller-rootviewcontroller

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