简体   繁体   English

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

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

I'm using a navigationController as the window.rootViewController .我使用navigationController作为window.rootViewController And as the rootViewController of navigationController , I'm using a tabBarController.作为navigationControllerrootViewController ,我使用的是 tabBarController。 But when I run the app, the title of the navigationBar is not showing up (My Tabbar Controller shows up and I'm being able to see my viewController. No problem about that) .但是当我运行应用程序时,navigationBar 的标题没有出现(我的 Tabbar Controller 出现了,我可以看到我的 viewController。没问题) I've tried to name the navigationBar in every viewController of the tabBar with using both of the code below in the viewDidload, but still nothing changed.我尝试使用 viewDidload 中的以下两个代码在 tabBar 的每个 viewController 中命名 navigationBar,但仍然没有任何改变。

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

I've set the navigationController with tabbar in the SceneDelegete as below:我在 SceneDelegete 中设置了带有标签栏的 navigationController,如下所示:

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

How can I show my navigationController title for every viewController of the tabBar?如何为 tabBar 的每个 viewController 显示我的 navigationController 标题?

Each view controller has a property called navigationItem of type UINavigationItem .每个视图 controller 都有一个名为navigationItem类型为UINavigationItem的属性。 If you set its title , it will show up in the navigation bar if one exists.如果您设置它的title ,它会在导航栏中显示(如果存在)。

For example in viewDidLoad , add this:例如在viewDidLoad中,添加:

self.navigationItem.title = "title"

In other words: The title is set on the view controller contained in a navigation controller stack, not the navigation controller itself.换句话说:标题是在导航 controller 堆栈中包含的视图 controller 上设置的,而不是导航 controller 本身。 Also not on the navigation bar, which probably also has a good reason I can't recall at the moment.也没有在导航栏上,这可能也是我现在想不起来的一个很好的理由。

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

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

相关问题 如何以编程方式显示具有情节提要中定义的导航控制器和标签栏控制器的控制器? - How to programmatically show a controller that has a navigation controller and tabbar controller as defined in a storyboard? 导航控制器未显示视图 - navigation controller doesn't show up the view 导航控制器干扰TabBar控制器 - Navigation Controller interfering with TabBar Controller 使用导航控制器时,关闭视图控制器不起作用 - Dismissing View Controller Doesn't Work While Using Navigation Controller 将导航控制器实现到标签栏控制器中 - Implement a navigation controller into a tabbar controller 将导航控制器放在标签栏控制器中 - Put a navigation controller in a tabbar controller TabBar 和导航控制器 - TabBar and Navigation Controller Swift:通过TabBar和导航控制器以编程方式从一个视图过渡到另一个视图 - Swift: Programmatically transitioning from 1 view to another via a TabBar and Navigation Controller 选项卡不会以编程方式切换导航控制器中的tabBar - tab does not switch programmatically for a tabBar inside a navigation controller 导航 controller navigationItems 没有按我的预期显示 - Navigation controller navigationItems doesn't show up as i expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM