3

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:

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)
aheze
  • 6,076
  • 3
  • 6
  • 40
michaeldebo
  • 2,107
  • 3
  • 9
  • 17

1 Answers1

0

Try calling rootVC.add(asChildViewController: tbc) before rootVC.beginAppearanceTransition(true, animated: false).

All your child VCs need to already be children of the parent before you call func beginAppearanceTransition(_: animated:)

AdamPro13
  • 6,534
  • 2
  • 26
  • 27