iOS 18. UINavigationController stack changes with delay

If I do something like this:

var viewControllers = navigationController.viewControllers
                if let lastViewController = viewControllers.popLast() {
                    navigationController.viewControllers = viewControllers
                        navigationController.pushViewController(lastViewController, animated: false)
                    }
                }

I got crash: pushing the same view controller instance more than once

If I set delay:

var viewControllers = navigationController.viewControllers
                if let lastViewController = viewControllers.popLast() {
                    navigationController.viewControllers = viewControllers
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                        navigationController.pushViewController(lastViewController, animated: false)
                    }
                }

it will work but with unnecessary transitions.

Should it work like this in iOS 18 ?

I encountered a similar problem, but my problem was that if two consecutive pushViewController operations were performed with animation, it would cause a crash. I have verified that there would be no crash if the animation was removed.

@CorsJJ I encountered a similar problem with setViewControllers, and your fix worked for me 🙏

use setViewControllers(...animated: false) in iOS 18 if seeing navigation issues.

iOS 18. UINavigationController stack changes with delay
 
 
Q