-1

I have 2 viewcontrollers (A & B).

A is auto rotation and B is force to landscape.

When user turn off Portrait Orientation Lock, it's work ok : A always rotation follow device orientation.

But when Portrait Orientation Lock is on, A's orientation is always landscape when it is returned from B.

How to force A is always portrait when Portrait Orientation Lock is enable.

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nguyen Hoan
  • 1,583
  • 1
  • 11
  • 18

2 Answers2

0

Here - https://stackoverflow.com/a/41811798/10261915 You have answer

Add this to AppDelegate

struct AppUtility {

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {

        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.orientationLock = orientation
        }
    }

    /// OPTIONAL Added method to adjust lock and rotate to the desired orientation
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {

        self.lockOrientation(orientation)

        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }

}

and use for example

AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)
JamesVoo
  • 145
  • 1
  • 11
0

add to BViewController :

override func viewDidLoad() {
        super.viewDidLoad()
   UIDevice.current.setValue(Int(UIInterfaceOrientation.landscapeLeft.rawValue), forKey: "orientation")
    }

    override func viewWillDisappear(_ animated: Bool) {
        UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
    }
bacat26
  • 11
  • 1
  • 3