简体   繁体   English

如何在UINavigationController rootViewController上禁用纵向方向

[英]How to disable portrait orientation on UINavigationController rootViewController

I have a class, HomeView, as the rootViewController of a UINavigationController on my iPad application. 我有一个类HomeView,作为iPad应用程序上UINavigationController的rootViewController。 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. 在info.plist文件中仅设置了横向方向,并且HomeView没有实现shouldRotateToInterfaceOrientation:但是在模拟器旋转时,视图在两种方向上都在旋转。

How do I only use the landscape orientations in my UIViewController? 如何只在UIViewController中使用横向方向?

If you do not implement shouldAutorotateToInterfaceOrientation: the runtime will call the method on the Super class, UIViewController . 如果未实现shouldAutorotateToInterfaceOrientation:运行时将在Super类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. 请参阅UIInterfaceOrientation参考页和UIViewController的shouldAutorotateToInterfaceOrientation:参考页,以获取更多信息。

You shouldn't remove the shouldAutorotateToInterfaceOrientation method; 您不应该删除shouldAutorotateToInterfaceOrientation方法。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使在横向方向,UINavigationController.view.frame也会返回纵向CGRect。 如何解决这个问题? - UINavigationController.view.frame returns portrait CGRect even in landscape orientation. How to fix this? 如何在 UINavigationController 的 RootViewController 上显示后退按钮? - How to show back button on the RootViewController of the UINavigationController? UINavigationController,带有UITabBarController的rootViewController - UINavigationController, rootViewController with UITabBarController 如何禁用UINavigationController? - How to disable UINavigationController? UINavigationController方向 - UINavigationController Orientation 如何从RootViewController禁用DetailViewController UIBarButtonItem? - How to disable a DetailViewController UIBarButtonItem from RootViewController? 在故事板中将uitableviewcontroller设置为uinavigationcontroller的rootviewcontroller - setting a uitableviewcontroller as rootviewcontroller for uinavigationcontroller in storyboard 禁用以纵向AVFoundation以外的任何方向拍摄图像 - Disable taking images in any orientation other than Portrait AVFoundation 如何仅将UIPopoverController限制为纵向方向? - How to restrict UIPopoverController for Portrait orientation only? 如果屏幕在纵向模式下被锁定,如何检测方向? - How to detect orientation, if screen is locked in portrait mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM