syrio
syrio

Reputation: 151

Preloading view from a UITabBar

I have a class that loads a UITabBarController. Each tab opens a UINavigationController.

I am trying to preload views inside my UINavigationControllers. I tried doing this:

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [navViewController loadView];
}

I tried different things but it never gets loaded. Am I doing something wrong?

Upvotes: 2

Views: 2396

Answers (4)

Moose
Moose

Reputation: 2737

extension UITabBarController {
    func preload() {
        children.forEach { _ = $0.view }
    }
}

Upvotes: 0

olafurr
olafurr

Reputation: 21

If you are using auto layout you have to tell the view to layout its subviews accordingly.

Try this

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers) {
     UIViewController *ctrl = navViewController.topViewController;
     [ctrl.view setNeedsLayout];
     [ctrl.view layoutIfNeeded];
}

Upvotes: 2

syrio
syrio

Reputation: 151

Thanks Or Arbel, you actually helped me understand the problem. I had to call view on the first UIViewController inside the UINavigationController. Here is the code that works:

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [[navViewController.viewControllers objectAtIndex:0] view];
}

Upvotes: 9

Or Arbel
Or Arbel

Reputation: 2975

Try

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [navViewController view];
}

Upvotes: 1

Related Questions