Forcing landscape is a common task for iOS games or video.
In your viewDidLoad
add the following lines:
override func viewDidLoad() {
super.viewDidLoad()
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
You’ll also need to add supportedInterfaceOrientations
:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
And shouldAutorotate
:
override var shouldAutorotate: Bool {
return true
}
In your AppDelegate
class define the property deviceOrientation
:
var deviceOrientation = UIInterfaceOrientationMask.landscape
Then also add a supportedInterfaceOrientationsFor
function in your AppDelegate
:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return deviceOrientation
}
Now your app should be in landscape. Note that you can choose .landscapeLeft
or .landscapeRight
.
The Complete iOS App Development Bootcamp
Disclosure: This website may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.