This appears to be a well documented problem, yet the solutions online have not worked. Here's just a sample list of posts that failed to provide me with a working answer:
- ViewWillAppear not executing code
- viewWillAppear not getting called
- viewWillAppear not called
- UINavigationController Inheritance, ViewWillAppear not called
- viewWillAppear not called after popToViewController
- iPhone viewWillAppear not firing
I have gleaned that my problem with viewWillAppear not getting called has to do with my view hierarchy. I am using a tab controller that is not the highest part of the view hierarchy. One of the tab controller's view controllers is a root view controller to a navigation controller. That's where I am trying to get a working viewWillAppear or viewDidAppear. Here's what I tried that has not worked. Within the tab controller I added this code:
let nav2 = UINavigationController(rootViewController: locationsVC)
nav2.beginAppearanceTransition(true, animated: false)
//...//
viewControllers = [ nav1, nav2, nav3, nav4 ]
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
for vc in self.children {
vc.beginAppearanceTransition(true, animated: animated)
}
}
In the scene delegate, this is my code:
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
let rootVC = NewOrExistingViewController()
rootVC.beginAppearanceTransition(true, animated: false)
let rootNC = UINavigationController(rootViewController: rootVC)
rootNC.navigationController?.navigationBar.isHidden = true
rootNC.beginAppearanceTransition(true, animated: false)
self.window?.rootViewController = rootNC
let tbc = TabBarViewController()
tbc.selectedIndex = 0
tbc.beginAppearanceTransition(true, animated: false)
rootVC.add(asChildViewController: tbc)