In one view controller: using a button action to force orientation (portrait to landscape and back), can each orientations be locked after rotation?

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.

Answered by coderkyle in 185748022

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.

Accepted Answer

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.

Not sure if it still applies, but in the past, devs were discouraged from detecting/using device orientation. UI is ok, otherwise.


You should never do any significant work inside of -shouldAutorotateToInterfaceOrientation:, as it is only used to query what orientations your view controller supports.


I would recommend to review the UIViewController documentation for a complete explaination of what the current various rotation methods are and how to use them.


If your navigation controller needs specific autorotation behavior, you should subclass UINavigationController and override supportedInterfaceOrientations,



Thank you so much for the help.


Once I created the global variable and passed it through the Button action, if finally worked correctly.

In one view controller: using a button action to force orientation (portrait to landscape and back), can each orientations be locked after rotation?
 
 
Q