如何将 NavigationDrawerController 和 PageTabBarController 添加到同一个项目

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()
 let pageTabBarController = AppPageTabBarController(viewControllers: [redViewController, greenViewController, blueViewController], selectedIndex: 0)
        let toolbarController = AppToolbarController(rootViewController: pageTabBarController)
        let menuController = AppMenuController(rootViewController: toolbarController)
        let navigationController  = AppNavigationDrawerController(rootViewController: menuController, leftViewController: leftViewController,rightViewController: rightViewController)

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

在此 中,我添加了有关导航控件的更多见解。您可能会发现它很有帮助。要直接回答您的问题,您正在寻找如下所示的代码设置:

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()
}
}

基本思想是将导航控制器(具有特定导航功能的控制器)分层堆叠在一起。您可以轻松地重新排列控制器以创建不同的流,并且您可以轻松地添加更多导航控制器,例如 SnackbarController。

视图控制器的堆叠顺序取决于您希望导航控制器的行为方式。例如,如果您将 MenuController 放在 ToolbarController 中,它会在您转换 ToolbarController 的 rootViewController 后转出。通过将它放在外面,您可以转换 ToolbarController,而 MenuController 保留在视图层次结构中。

需要注意的一条规则是,您应始终将 NavigationDrawerController 放置在最外层的导航控件中,因为 leftright 控制器应始终与所有其他控件重叠。

希望对您有所帮助:)