繁体   English   中英

导航 Controller 在使用 TabBar 控制器时不显示标题(以编程方式)

[英]Navigation Controller doesn't show the title while working with TabBar Controller(Programmatically)

我使用navigationController作为window.rootViewController 作为navigationControllerrootViewController ,我使用的是 tabBarController。 但是当我运行应用程序时,navigationBar 的标题没有出现(我的 Tabbar Controller 出现了,我可以看到我的 viewController。没问题) 我尝试使用 viewDidload 中的以下两个代码在 tabBar 的每个 viewController 中命名 navigationBar,但仍然没有任何改变。

 navigationController?.title = "title"
 navigationController?.navigationBar.topItem?.title = "title"

我在 SceneDelegete 中设置了带有标签栏的 navigationController,如下所示:

let countriesTabbarController = TabBarControllerBuilder.build()
    
    let navigationController = UINavigationController(rootViewController: countriesTabbarController)
    
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()

如何为 tabBar 的每个 viewController 显示我的 navigationController 标题?

每个视图 controller 都有一个名为navigationItem类型为UINavigationItem的属性。 如果您设置它的title ,它会在导航栏中显示(如果存在)。

例如在viewDidLoad中,添加:

self.navigationItem.title = "title"

换句话说:标题是在导航 controller 堆栈中包含的视图 controller 上设置的,而不是导航 controller 本身。 也没有在导航栏上,这可能也是我现在想不起来的一个很好的理由。

https://developer.apple.com/documentation/uikit/uinavigationitem

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM