iOS Swift UIViewController Rotation

98 views Asked by At

I have a UITabViewController with three tabs. On one of the tabs, the user taps a button and a new UIViewController controller pops up in Landscape mode (for the user to sign their name using their finger). After the user is finished, they tap a Finish button and finish() is called which closes the signing UIViewController.

The problem I am facing is the screen that the user returns to that was in Portrait mode is now in Landscape mode even though the screen is set:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    return .portrait
}

override var shouldAutorotate: Bool {
    return true
}

and there nothing I can do to force rotation back to portrait. I have read posts such as: How to force device rotation in Swift? and as I understand it, there is nothing I can do to rotate the screen back to portrait?

1

There are 1 answers

0
SAIF On

It's really simple. Write the following code and change orientation value in ViewControllers you want to programmatically rotate.

override func viewWillAppear(_ animated: Bool) {
        let value = UIInterfaceOrientation.portrait.rawValue //Change orientation according to your scenario
        UIDevice.current.setValue(value, forKey: "orientation")
    }