简体   繁体   中英

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. (VC2B has a navigation bar)

I have tried different types of code but without luck. Ideally I would like to to enable rotation in only 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.

Allow all orientations for only one 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 ?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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