The orientation of the locking device when viewing does not work when returning to NavBar

I have an application with several VCs built into the NavigationController.

I need all VCs to be locked in portrait orientation and only one in portrait and landscape.

I tried different approaches and this one works for me:

extension UINavigationController { public override func supportedInterfaceOrientations() -> Int { return visibleViewController.supportedInterfaceOrientations() } public override func shouldAutorotate() -> Bool { return visibleViewController.shouldAutorotate() } } 

And then I modify each ViewController as follows:

 class ViewController: UIViewController { override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Portrait.rawValue) } } 

Everything is fine when I open the application and move from one VC to another. Representations, which should be only "Portrait", are only "Portrait". Then I open a view that has not been changed, and it changes the Orientation interface, as intended.

BUT , then let me say that I leave the last unmodified view in the Landscape, and I press the button back in the NavBar and go to the previous controller (which was changed only for Portrait). The VC layout is shown as Landscape.

And if I continue to press the "Back" button, all other VCs behave as if they were never told only about the portrait.

I used this post Corey Hinton, Screen rotation lock

+2
source share
2 answers

I think I was able to reproduce the scenario you described. Unfortunately, the usual ways that I am trying to get it to update did not work, but I found this SO answer that seems to work. Put this in the view controller, which you will return back to viewDidAppear :

 override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let value = UIInterfaceOrientation.Portrait.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") } 

This fix seems to have worked for me. However, this is a rather rough transition. Hope this helps

+1
source

I had this problem with the view controllers represented and orientation blocking differences. I added this to the "unlocked" controller:

 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self willRotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft duration:0]; } 

So, when this controller is removed (the button is pressed back), it sends a message that it should return to the landscape (it does not matter left or right, because the penetrating controller will autorotate from this in the correct orientation). This is in objective-c, but I'm sure there is a similar operation in QuickTime. You may have to deal with the duration of the animation if it makes things look weird, but mine worked just fine with immediate switching.

0
source

All Articles