I have many View Controllers in my ios App and they all support rotation but for one of my scenes (and only one), I would like to detect the trait environment and run it only if the device trait environment is regular height (so an iPhone in portrait or an iPad in both portrait or landscape). How to achieve this? So I want to get the vertical size class and based on which I want to freeze the orientation. So if verticalSizeClass == .compact, the orientation of views of the UIViewController should be portrait else the orientation of the views of the UIViewController can be the same as that of the screen. I am using the following code
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
let tc = UIScreen.main.traitCollection
let orientation: UIInterfaceOrientation = UIApplication.shared.statusBarOrientation
if tc.verticalSizeClass == .compact {
return .portrait
} else {
return orientation
}
}
But it's giving error messages for autolayout. I am using Xcode 10.2, running Swift 5.0. There is a question How to force view controller orientation in iOS 8? but it is from objective c perspective and more importantly I want the viewController to rotate as per the device, while the question "How to force view controller orientation in iOS 8?" locks the orientation to portrait or landscape and does not care about landscape left or right.