How to load all views in UITabBarController?

Sam picture Sam · Oct 21, 2015 · Viewed 10.2k times · Source

I have not found relevant and up to date answers in the posts related to this question.

I would like to load all viewcontrollers on launch. Currently it launches as expected but when I tap on a bar item (the first time) there is a slight delay to load it because it has not been loaded yet.

How can I do that is Swift ?

Thanks.

Answer

Robert picture Robert · Oct 21, 2015

To preload a UIViewController's view, simply access its view property:

let _ = myViewController.view

To preload all view controllers on a UITabBarController, you can do:

if let viewControllers = tabBarController.viewControllers {
    for viewController in viewControllers {
        let _ = viewController.view
    }
}

Or a bit more compactly:

tabBarController.viewControllers?.forEach { let _ = $0.view }