How to restore right orientation of the UIViewController after orientation was forced to landscape by another (iOS)?

330 Views Asked by At

The problem I am having is a bit strange and I can't seem to find any good solutions out there. So I have two UIViewControllers, one is allowed to take all orientations, another can only be viewed in landscape

internal override func shouldAutorotate() -> Bool {
    return true
}

internal override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Lanscape
}

If I am in portrait mode on ViewController1, and I push VC2, it rotates just fine, but when I leave VC2, VC1 is stuck in landscape even though device itself is in portrait mode. How can I fix it? I tried calling AttemptToRotateToDeviceOrientation method in ViewDidAppear method of VC1, but it doesn't do anything

1

There are 1 best solutions below

2
Munzareen Atique On

You just need to create app delegate instance in your view controller like:

let appDel = UIApplication.shared.delegate as! AppDelegate

On Viewwillappear()of view controller just add this code for landscape

appDel.myOrientation = .landscape
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

And for portrait add this code

appDel.myOrientation = .portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")