iOS. How to enable and disable rotation on each UIViewController?

后端 未结 8 2118
栀梦
栀梦 2020-12-13 04:10

I have an UIViewController, I want to disable or enable rotation of the screen in different scenarios

Example:

if flag {
   rotateDevice = false
}
e         


        
相关标签:
8条回答
  • 2020-12-13 05:01

    You simply have to implement shouldAutorotate and supportedInterfaceOrientations into your UIViewController. Take a look at this documentation. For example:

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

    You can also specify which orientations are available. Here is an example with only portraits orientations:

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
    

    Edit: If you want to support different orientation regarding a flag, you just have to do something like this:

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if myFlag {
            return .Landscape
        } else {
            return .All
        }
    }
    

    (If myFlag is true, it will allow Landscape orientation. Otherwise, it will allow all orientations).

    0 讨论(0)
  • 2020-12-13 05:01

    set device orientarion portrait in Target -> General -> Deployment Info -> Portrait

    0 讨论(0)
提交回复
热议问题