I want to turn off the device rotation programmatically. I tried the below solution but this doesn’t work for me.
private func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
}
}
2
Answers
I’m assuming your default rotation is .
portrait
. So in AppDelegate file, conformsupportedInterfaceOrientationsFor
then declare a propertyUIInterfaceOrientationMask
, set it to.portrait
.The
supportedInterfaceOrientationsForWindow
method you mentioned is outdated. Instead, you should use thesupportedInterfaceOrientations
method in your view controller to control the supported interface orientations.Should look like this:
Make sure you override the
supportedInterfaceOrientations
property in the view controller where you want to control the rotation behavior. This will allow you to specify whether the view controller supports portrait or landscape orientations based on yourshouldRotate
flag.Also, ensure that you have set the correct initial orientation settings for your app in your project settings. You can do this in Xcode by selecting your project, going to the "General" tab, and configuring the "Device Orientation" settings to match your desired behavior.
I hope I could help.