UINavigationBar layoutSubviews crash

I'm running my App in iOS17 and the navigation bar crashes when I switch the root view of my window. I don't know what happened, I'm sure I wrote everything correctly. Below is my crash log.

I would need to see the exception text to be certain, but the most likely cause is that you've managed to get the same UINavigationItem placed in two different UINavigationBars. This can create a layout loop (where the two navigation bars continually fight for content produced by the navigation item) leading to a hang/watch dog crash.

Since you say this is occurring when you switch the root view of your window, this seems like the most likely cause.

This happens to me when I use side bar menu on iPad. When I tap on an active side bar item second time the app crashes.
I fixed the crash by checking if the view controller is already on screen like this:

if splitViewController.viewController(for: .secondary) != selectedViewController {
    splitViewController.setViewController(nil, for: .secondary)
    splitViewController.setViewController(selectedViewController, for: .secondary)
}

I hope this information might help someone to fix the issue in similar situation.

UINavigationBar layoutSubviews crash
 
 
Q