-1

My app is landscape mode except for one single VC. TopVC is landscape but when loading MyVC it should be rotate according to device orientation. Loading MyVC working as expect but returning to TopVC is not showing correctly if the mode portrait(landscape mode is fine).
here is the code I tried

in AppDelegate.swift

var orientationLock = UIInterfaceOrientationMask.landscape
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {        
    return self.orientationLock
}

func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
    self.orientationLock = orientation
}

in MyVC.swift

override func viewWillAppear(_ animated: Bool) {
    // change orientation
    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        delegate.lockOrientation(.allButUpsideDown)
    }
}
override func viewWillDisappear(_ animated: Bool) {        
    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        delegate.lockOrientation(.landscapeLeft)
    }
}

Can someone help me to resolve this? Problem is return back to TopVC showing in portrait mode if it returning portrait mode from MyVC.

mmk
  • 182
  • 3
  • 18
  • 1
    Check this out https://stackoverflow.com/questions/27037839/force-landscape-mode-in-one-viewcontroller-using-swift – Shreeram Bhat Dec 08 '21 at 06:26
  • @ShreeramBhat thank you for your support, I have already tried that one and below answer worked. https://stackoverflow.com/a/58424565/1835679 – mmk Dec 08 '21 at 07:51

1 Answers1

0

if somebody needs an answer, try this. worked for me.

override func viewWillDisappear(_ animated: Bool) {        
    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        delegate.lockOrientation(.landscape)
    }
    if !UIWindow.isLandscape {
        UIDevice.current.setValue(Int(UIInterfaceOrientation.landscapeLeft.rawValue), forKey: "orientation")
    }
}
mmk
  • 182
  • 3
  • 18