Is there any way to detect the screen orientation even when the user has locked his orientation? My App uses a custom camera view and I would like to be able to save images in either landscape or portrait depending on the orientation. I am currently using the following code to detect the orientation, however this only works if the user has not locked it.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let orientation: UIDeviceOrientation = UIDevice.current.orientation
switch (orientation) {
case .portrait:
print("Portrait")
case .landscapeRight:
print("Left")
case .landscapeLeft:
print("Right")
default:break
}
}
If this is not the correct way to work with the camera orientation, please guide me in the right direction.
I believe that the only way to do this is enabling both portrait and landscape mode in project:
Then, override these properties in all your view controllers that don't need a landscape mode.
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
In the view controller with embedded camera just set shouldAutorotate false and add extra supportedInterfaceOrientations that are used in landscape.
Note that if you use UINavigationController or UITabBarController it will not work, so you should create these extensions: https://stackoverflow.com/a/39674618/5381663