要将屏幕导航逻辑与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
如何解决?
谢谢
答案 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()
}
}