How to lock Screen Orientation programmatically in iOS?



You might come across a scenario where you need to show the UI in a specific orientation may be Landscape or Portrait.

We will be seeing how to lock orientation programmatically using Swift in iOS.

Open Xcode → New Project → ViewController.swift write the below code.

// Set the shouldAutorotate to False
override open var shouldAutorotate: Bool {
   return false
}

// Specify the orientation.
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
   return .portrait
}


Advertisements