简体   繁体   English

仅对一个UIViewController允许多个方向

[英]Allow multiple orientations for only one UIViewController

I want the general orientation of my app to be portrait, but for one specific UIViewController (VC2B) I want the user to be able to tilt the device for landscape orientation. 我希望我的应用程序的总体方向是纵向,但是对于一个特定的UIViewController (VC2B),我希望用户能够倾斜设备以实现横向方向。 (VC2B has a navigation bar) (VC2B有一个导航栏)

I have tried different types of code but without luck. 我尝试了不同类型的代码,但是没有运气。 Ideally I would like to to enable rotation in only UIViewController (VC2B). 理想情况下,我只想在UIViewController (VC2B)中启用旋转。 But this seems impossible, instead it seems you have to enable all the orientations in the General section and then turn them off programatically - is this really true? 但这似乎是不可能的,相反,您似乎必须启用“常规”部分中的所有方向,然后以编程方式将其关闭-这是真的吗?

Anyway I have tried various things 无论如何,我尝试过各种东西

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientation.LandscapeLeft.rawValue) | Int(UIInterfaceOrientation.LandscapeRight.rawValue) | Int(UIInterfaceOrientation.Portrait.rawValue)
}

The below answer suggest overriding shouldAutorotate to false, but that does not work if the page is loaded in landscape mode, then it is just stuck on landscape. 下面的答案建议覆盖应该将shouldAutorotate为false,但是如果以横向模式加载页面,则该页面仅停留在横向模式下不起作用。

Allow all orientations for only one view controller 只允许一个View Controller的所有方向

Question : How can I in a tidy way, without adding code to all my UIViewController s, allow multiple orientations for only one UIViewController ? 问题 :如何在不向我的所有 UIViewController添加代码的情况下,以整齐的方式仅对一个UIViewController允许多个方向?

Do the following as I did this when I faced same situation: 遇到相同情况时,请执行以下操作:

enable the desired orientations in the General Info Screen in each ViewController.swift, override the autorotation function with either true or false (true for the one that should rotate, false for the others) 在每个ViewController.swift的“常规信息”屏幕中启用所需的方向,使用truefalse 覆盖自动旋转功能(对于应该旋转的一个,为true;对于其他旋转的,则为false)

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

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