0

I have a view Controller VC1 which can autorotate to all orientations. I set the interface manually by activating and deactivating appropriate autolayout constraints. On autorotation, I do:

 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    let orientation = UIApplication.shared.statusBarOrientation

    super.viewWillTransition(to: size, with: coordinator)



    coordinator.animate(alongsideTransition: { [unowned self] (_) in

        let orient = UIApplication.shared.statusBarOrientation


        self.layoutInterfaceForOrientation(orient)

    }, completion: { [unowned self] (UIViewControllerTransitionCoordinatorContext) -> Void in

        let orient = UIApplication.shared.statusBarOrientation

        NSLog("rotation completed to \(orient.rawValue)")
        self.layoutInterfaceForOrientation(orient)
    })

}

Now comes the problem. I have VC2 which is invoked via a segue from VC1 by a touch of a button. VC2 supports only landscape mode. So if my VC1 is in portrait mode and the segue gets triggers, VC1 screen is still in portrait mode but starts laying out elements as if the screen is in landscape mode and all my UI looks messed up & garbled while segue is in transition. This is because UIApplication.shared.statusBarOrientation returns landscape mode as the segue is in transition but iPhone is still held vertical.

I tried many things, such as having a flag for segue in progress and disabling autorotation when the flag is set, but no effect. Any ideas?

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
  • Possible duplicate: [How to rotate orientation programmatically in Swift?](https://stackoverflow.com/questions/32345409/how-to-rotate-orientation-programmatically-in-swift) – Jay Lee Jul 28 '19 at 07:07
  • No I don't think it's the same – Deepak Sharma Jul 28 '19 at 07:16
  • You should not be basing anything on the status bar orientation. Base your layout on view size. – rmaddy Jul 28 '19 at 07:20
  • Even if I do that, there is no change. My layoutInterfaceForOrientation() does not gets triggered on autorotation if I return from it manually when segue is in progress. But still interface elements are messed up. Which means internally view controller is autorotating. – Deepak Sharma Jul 28 '19 at 07:49

0 Answers0