Search code examples
swiftcosmicmind

How do I add NavigationDrawerController and PageTabBarController to the same project


  let pageTabBarController = AppPageTabBarController(viewControllers: [redViewController, greenViewController, blueViewController], selectedIndex: 0)
        let toolbarController = AppToolbarController(rootViewController: pageTabBarController)
        let menuController = AppMenuController(rootViewController: toolbarController)

        window = UIWindow(frame: Device.bounds)
        window!.rootViewController = menuController
        window!.makeKeyAndVisible()

Solution

  • In this stackoverflow question I add more insight about navigational controls. You may find it helpful. To directly answer your question, you are looking for a code setup that looks like this:

    import UIKit
    import Material
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func applicationDidFinishLaunching(_ application: UIApplication) {
        let redViewController = RedViewController()
        let greenViewController = GreenViewController()
        let blueViewController = BlueViewController()
    
        let pageTabBarController = AppPageTabBarController(viewControllers: [redViewController, greenViewController, blueViewController], selectedIndex: 0)
        let toolbarController = AppToolbarController(rootViewController: pageTabBarController)
        let menuController = AppMenuController(rootViewController: toolbarController)
    
        let leftViewController = LeftViewController()
        let rightViewController = RightViewController()
    
        let navigationDrawerController = NavigationDrawerController(rootViewController: menuController,
                                                                    leftViewController: leftViewController,
                                                                    rightViewController: rightViewController)
    
        window = UIWindow(frame: Device.bounds)
        window!.rootViewController = navigationDrawerController
        window!.makeKeyAndVisible()
    }
    }
    

    The basic idea is that you are stacking your navigational controllers (controllers that have a specific navigational feature) on top of each other in layers. You can easily rearrange the controllers to create different flows and you can easily add more navigational controllers, such as the SnackbarController.

    The order in which you stack the view controllers depends on how you want the navigational controllers to behave. For example, if you put the MenuController within the ToolbarController, it will transition out once you transition the rootViewController of the ToolbarController. By placing it on the outside, you can transition the ToolbarController and the MenuController remains within the view hierarchy.

    One rule to note, is you should always place the NavigationDrawerController as the outer most navigational control, as the left and right controllers should always overlap all other controls.

    Hope that helps :)