Search code examples
iosxcodeswift4autorotate

How to allow only single UIViewController to rotate upside down with Xcode?


In my app all screens have portrait orientation. How to allow only single UIViewController be in two orientation mode: portrait and upside down? When user rotates an iPhone, the UIViewController should rotates too.


Solution

  • First of all, In your application all screens except one screen are in portrait orientation. So You cannot set orientation of your application as portrait. So Set Device Orientation as portrait and LandScape both.

    Put following code on which screen you need landscape orientation in ViewDidLoad

    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
    

    and

    override var shouldAutorotate: Bool {
    return true
    }
    

    Put following code on all screens where you need only portrait orientation in viewDidLoad

    let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")