swift – Tips on how to disable bizarre rotation animations when setting UINavigationController.viewControllers with completely different orientations in iOS 16

[ad_1]

The title could also be complicated, so I’ll clarify with an instance:

  1. There are 3 view controllers within the navigation stack: FirstViewController, SecondViewController and ThirdViewController.
  2. FirstViewController helps panorama interface orientation solely whereas SecondViewController and ThirdViewController help portrait solely.
  3. When FirstViewController reveals up, SecondViewController will likely be pushed into the stack after 2 seconds, and we are able to see a rotation animation from panorama to portrait.
  4. Then I proceed to push a ThirdViewController occasion and pop SecondViewController on the similar time by assigning a view controller array to navigation controller.

The issue is in Step 4: For iOS 16, there’s additionally a rotation animation for ThirdViewController, similar to when SecondViewController is pushed into the stack.

enter image description here

class CustomNavigationController: UINavigationController {
    override var shouldAutorotate: Bool {
        return topViewController?.shouldAutorotate ?? false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return topViewController?.supportedInterfaceOrientations ?? .portrait
    }
}

class FirstViewController: UIViewController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeRight
    }
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        title = "First"
        view.backgroundColor = .inexperienced
        DispatchQueue.fundamental.asyncAfter(deadline: .now() + 2) {
            let vc = SecondViewController()
            self.navigationController?.pushViewController(vc, animated:true)
        }
    }
}

class SecondViewController: UIViewController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        title = "Second"
        view.backgroundColor = .yellow
        DispatchQueue.fundamental.asyncAfter(deadline: .now() + 2) {
            let vc = ThirdViewController()
            var viewControllers = self.navigationController?.viewControllers ?? []
            viewControllers.removeLast()
            viewControllers.append(vc)
            // setting animated to false would not assist
            self.navigationController?.setViewControllers(viewControllers, animated: true) 
        }
    }
}

class ThirdViewController: UIViewController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        title = "Third"
        view.backgroundColor = .pink
    }
}

Is there any solution to remedy this bizarre rotation animation on iOS 16?

[ad_2]

Leave a Reply