Orientation question: In one view controller with no navigation controller. I am forcing landscape orientation when the back camera is detected and portrait when the switch to the front camera happens. Is it possible to lock each orientation after it's been rotated? Right now, even after forcing the orientation, the view controller will not stay there. Since rotation is still enabled, and there are two possible orientations on this view controller, the view controller rolls to the other possible view. This will be connected to video so it all has to stay in one view controller. If what I am trying to do is not possible, please let me know.
The code below is tied to a button function that swaps cameras:
func cameraPosition(cameraDevice: AVCaptureDevice)
{
let frontCamera = AVCaptureDevicePosition.front;
let backCamera = AVCaptureDevicePosition.back;
if (cameraDevice.position == backCamera)
{
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
else if(cameraDevice.position == frontCamera)
{
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
}
Thanks for taking the time to read this.
I'm not sure if there is a correct way to do it, but I would do it like this:
1. Create a global Bool variable
2. When you click the button you mentioned, switch boolean value to true or false
Something like:
If TorF == true {
TorF = false
} else {
TorF = true
}
3. Then add following functions to your view controller:
override var shouldAutorotate : Bool {
return false
}
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
if TorF == true {
return UIInterfaceOrientationMask.portrait
} else {
return UIInterfaceOrientationMask.landscape
}
}
Let me know if this works for you, or it might be that you may only need to add the "shouldAutorotate" since you are already changing the orientation in your button function.