简体   繁体   English

如何在标签内的视图控制器中设置导航栏标题?

[英]How to set the navigation bar title from view controllers inside tabs?

I have a tab bar controller embedded in a navigation controller. 我在导航控制器中嵌入了标签栏控制器。 I want to set the title in the navigation from the view controllers inside the tabs. 我想在选项卡内的视图控制器中在导航中设置标题。

If I use: 如果我使用:

self.title = "ABC";

or 要么

self.tabBarItem.title = "ABC";

the title of the tab at the bottom is set to "ABC", but not the title in the navigation bar at the top. 底部标签的标题设置为“ ABC”,而不是顶部导航栏中的标题。

However, I only want to set the title in the navigation bar, not the tabs at the bottom. 但是,我只想在导航栏中设置标题,而不是底部的标签。

How can I do that? 我怎样才能做到这一点?

在此处输入图片说明

您可以使用self.navigationItem.title进行设置。

self.navigationItem.title=@"ABC";

You have to pass the navigationItem up the chain. 您必须将navigationItem向上传递。

The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController. UINavigationController显示属于其topViewController的navigationItem,它是UITabBarController。

The UITabBarController shows the navigationItem titles in its tabs. UITabBarController在其选项卡中显示navigationItem标题。 So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem 因此,您需要做的是确保tabBarController的navigationItem是selectedViewController的navigationItem

So to recap: 总结一下:

UINavigationController title = topViewController.navigationItem.title

UITabBarController tabTitle = selectedViewController.navigationItem.title

UIViewController title = navigationItem.title

您可能使用了错误的元素,请尝试以下示例:

self.tabBarController.title = @"YOUR_TITLE";

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

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