Fix crash due to nested navigation controllers

This commit is contained in:
Shadowfacts 2022-07-11 14:59:01 -04:00
parent ea07e6aef6
commit 4fa1bd7268
1 changed files with 13 additions and 2 deletions

View File

@ -67,6 +67,17 @@ class SplitNavigationController: UIViewController {
if let rootViewController { if let rootViewController {
rootNav.viewControllers = [rootViewController] rootNav.viewControllers = [rootViewController]
} }
// add the child VCs here, rather than in viewDidLoad, because this VC is added to the UISplitViewController,
// it needs a UINavigationController to be this VC's first child, otherwise it will embed this VC inside
// yet another UINavigationController, which can then cause a crash when we try to embed a nav controller inside
// of ourself (because nested nav controllers are forbidden)
rootNav.willMove(toParent: self)
addChild(rootNav)
rootNav.didMove(toParent: self)
secondaryNav.willMove(toParent: self)
addChild(secondaryNav)
secondaryNav.didMove(toParent: self)
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
@ -76,10 +87,10 @@ class SplitNavigationController: UIViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
embedChild(rootNav, layout: false)
embedChild(secondaryNav, layout: false)
rootNav.view.translatesAutoresizingMaskIntoConstraints = false rootNav.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(rootNav.view)
secondaryNav.view.translatesAutoresizingMaskIntoConstraints = false secondaryNav.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(secondaryNav.view)
separatorView.backgroundColor = .separator separatorView.backgroundColor = .separator
separatorView.translatesAutoresizingMaskIntoConstraints = false separatorView.translatesAutoresizingMaskIntoConstraints = false