Search code examples
iosswiftcosmicmind

How can I use NavigationController and PageTabBarController at the same time?


I'm sorry to bother you and I really love Material Project, I hope I could use it skillfully and make a prettier app. I wonder if I could know how can I use NavigationController and PageTabBarController at the same time, since if I want to use one of them, I have to set it in the AppDelegate as the rootViewController, then what should I do to the other one? Thanks.


Solution

  • I am glad you are enjoying Material :)

    Take a look at this article Application Architecture with Material.

    Basically, you can set the PageTabBarController as the rootViewController of the NavigationController, like this:

    import UIKit
    import Material
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
    
        func applicationDidFinishLaunching(_ application: UIApplication) {
            let pageTabBarController = AppPageTabBarController(viewControllers: [RedViewController(), GreenViewController(), BlueViewController()], selectedIndex: 0)
    
            window = UIWindow(frame: Screen.bounds)
            window!.rootViewController = AppNavigationController(rootViewController: pageTabBarController)
            window!.makeKeyAndVisible()
        }
    }
    

    That should do it. All the best.