krzysztofzablocki / Inject

Hot Reloading for Swift applications!
MIT License
2.1k stars 114 forks source link

Setup with UITabBarController #89

Closed everlof closed 4 months ago

everlof commented 4 months ago

Is this correctly setup for a UITabBarController in your view?

        let window = UIWindow(windowScene: windowScene)

        let dashboardViewController = Inject.ViewControllerHost(DashboardViewController())
        dashboardViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)

        let historyViewController = Inject.ViewControllerHost(HistoryViewContrller())
        historyViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .recents, tag: 1)

        let tabBarController = Inject.ViewControllerHost(UITabBarController())
        tabBarController.viewControllers = [
            dashboardViewController,
            historyViewController,
        ]

        window.rootViewController = tabBarController

For me it seems to break completely and just show a fully gray tabbar after a reload.

krzysztofzablocki commented 4 months ago

I'd try making a function that creates the final VC and use that function as ViewControllerHost param fully configuring your original VC there:

        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = Inject.ViewControllerHost(configuredTabBarController())

        func configuredTabBarController() -> UITabBarController {
          let dashboardViewController = DashboardViewController()
          dashboardViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)

          let historyViewController = HistoryViewController()
          historyViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .recents, tag: 1)

          let tabBarController = UITabBarController()
          tabBarController.viewControllers = [
            dashboardViewController,
            historyViewController,
          ]
          return tabBarController
        }
everlof commented 4 months ago

Great input, thanks, work like a charm!