Set Orientation Programmatically (Swift)

Nitish Kumar
Dec 7, 2021

--

In this article we will learn device orientation issue and how to resolve that.

In case, if your device orientation change while dismissing view controller and you already settled your orientation as given below image but still not working.

This checked will work only while presenting view controller.

Now, to solve this problem we use two steps:

Step 1 : Create a class with static function.

class OrientationManager {

static var landscapeSupported: Bool = false

static func setOrientation(_ orientation: UIInterfaceOrientation) {

let orientationValue = orientation.rawValue

UIDevice.current.setValue(orientationValue, forKey: “orientation”)

landscapeSupported = orientation.isLandscape

}

}

Step 2 : Call this static function in viewWillDisappear for all View Controller.

override func viewWillDisappear(_ animated: Bool) {

OrientationManager.setOrientation(.portrait)

}

Hope you enjoyed reading this post!!

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

Thank you!!! Happy coding.

--

--

Nitish Kumar
Nitish Kumar

Written by Nitish Kumar

I developed and maintain applications aimed at a range of iOS devices including mobile phones and tablet computers.

No responses yet