Home > Software engineering >  How to allow only single UIViewController to rotate upside down with Xcode?
How to allow only single UIViewController to rotate upside down with Xcode?

Time:10-21

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.

CodePudding user response:

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")

CodePudding user response:

If you have any function that continuously called then you can check the device orientation inside this function as-

if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
    // set your view constraints
}

if UIDevice.current.orientation == UIDeviceOrientation.portrait {
    // set your view constraints 
}

Otherwise you should create a monitoring function and check device orientation inside the function.

  • Related