I have an app which was developed to run in Portrait orientation only.
Prior to iOS 10 and Swift 3, I could use the following code in the ViewController -
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
override func shouldAutorotate() -> Bool {
return true
}
However after migrating my project to Swift 3 in Xcode 8 (Beta 6), the above code shows the error - “Method does not override any method from its superclass”, and does not compile.
So I checked the API Reference, and it appears that shouldAutorotate and supportedInterfaceOrientations are now defined as get-only properties for ViewControllers in Swift as follows -
var shouldAutorotate: Bool { get }
var supportedInterfaceOrientations: UIInterfaceOrientationMask { get }
So how can we set these values if they are get-only properties? I tried the following in the the AppDelegate’s didFinishLaunchingWithOptions function -
if let vController = self.window?.rootViewController as? ViewController{
vController.shouldAutorotate = true
}
But it gives an error message saying - Cannot assign to property: ‘shouldAutorotate’ is a get-only property
Can anyone shed some light on how to configure the shouldAutorotate and supportedInterfaceOrientations parameters for individual ViewControllers?
Since they have changed from funcs to vars, you also need to override them as vars. Get-only (read-only) vars can be written without the get{} specifier if you want to save a few keystrokes:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
override var shouldAutorotate: Bool {
return false
}