现在使用协调器导航显示导航栏

时间:2019-11-18 03:34:09

标签: ios swift uinavigationcontroller uinavigationbar

要将屏幕导航逻辑与UIViewController分开,请遵循本教程: https://www.hackingwithswift.com/articles/71/how-to-use-the-coordinator-pattern-in-ios-apps

使用Coordinator protocol进行的所有设置均正确设置,但导航栏未显示。

Coordinator.swift

protocol Coordinator {
    var childCoordinator: [Coordinator] {get set}
    var navigationController: UINavigationController {get set}

    func start()
}

MainCoordinator.swift

class MainCoordinator: Coordinator {
    var childCoordinator = [Coordinator]()

    var navigationController: UINavigationController

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }
    func start() {
        let vc = ViewController.instantiate()
        navigationController.pushViewController(vc, animated: false)
        navigationController.title = "Home"
        navigationController.navigationBar.prefersLargeTitles = true

    }
}

完整的源代码可以在GitHub上找到: https://github.com/harshal-wani/CoordinatorNavigation

如何解决?

谢谢

2 个答案:

答案 0 :(得分:0)

您将title设置为navigationController而不是ViewController,因此您无法在存在的title上看到navigationBar。将title设置为

func start() {
    let vc = ViewController.instantiate()
    vc.title = "Home"
    navigationController.pushViewController(vc, animated: false)
    navigationController.navigationBar.prefersLargeTitles = true  // 4

}

答案 1 :(得分:0)

您也可以如下修改场景委托:

var window: UIWindow?
var coordinator: MainCoordinator?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    let navController = UINavigationController()
    coordinator = MainCoordinator(navigationController: navController)
    coordinator?.start()

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = navController
        self.window = window
        window.makeKeyAndVisible()
    }
}