swift – The best way to disable bizarre rotation animations when setting UINavigationController.viewControllers with totally 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 assist 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 identical time by assigning a view controller array to navigation controller.

The issue is in Step 4: For iOS 16, there may be additionally a rotation animation for ThirdViewController, identical 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.most important.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.most important.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 technique to resolve this bizarre rotation animation on iOS 16?

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *