0
votes

I've got a UITabBarController embedded in a UIViewController. I'm trying to access the UITabBarController instance from the parent UIViewController.

vc.children has a return type of [UIViewController] so the UITabBarController doesn't show up.

vc.tabBarController is nil because the view is not embedded in the TabBarController. My setup is the other way around.

Any ideas?

My storyboard: storyboard

2

2 Answers

0
votes

Fastest solution:
vc.children.compactMap({$0 as? UITabBarController}).first .

Best solution:
Select the Embed Segue from the storyboard and give an identifier (say "containerEmbedSegue". Next, in your vc:

var tabBarVC: UITabBarController?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "containerEmbedSegue" {
        self.tabBarVC = segue.destination as? UITabBarController
    }
}
0
votes

I needed to move window?.rootViewController = viewController before vc.children.