ios 如何独立于标签栏项目标题更改 uiviewcontroller 标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21615637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:22:22  来源:igfitidea点击:

how to change uiviewcontroller title independent of tabbar item title

ioscocoa-touchuiviewcontrolleruitabbarcontroller

提问by Atma

I am setting my view controllers title like this in view did load:

我在视图中设置我的视图控制器标题是这样的:

self.title = @"my title";

prior to this I set the title in story boards for the view controller and navigation controller it is embedded in. I set it to: "Title";

在此之前,我在故事板中为它嵌入的视图控制器和导航控制器设置了标题。我将其设置为:“标题”;

When I click on the tab that holds the view controller the title of tab bar item and uiviewcontrollerchange to: my title

当我单击包含视图控制器的选项卡时,选项卡栏项目的标题并uiviewcontroller更改为:我的标题

I would like for the view controller to change but the tab bar item to stay with the title: Title

我希望视图控制器改变,但标签栏项目保持标题:标题

How can I accomplish this?

我怎样才能做到这一点?

回答by Craig Siemens

It sounds like you want the title in the navigation bar to change but not the one in the tabbar. This should do that.

听起来您希望导航栏中的标题发生变化,而不是标签栏中的标题。这应该这样做。

[self.navigationItem setTitle:@"my title"];

Swift:

迅速:

self.navigationItem.title = "My Title"

回答by Simon Epskamp

So for those who still don't get it (like me)

所以对于那些仍然不明白的人(比如我)

self.navigationItem.title = @"my title";sets navigation bartitle.

self.navigationItem.title = @"my title";设置导航栏标题。

self.tabBarItem.title = @"my title";sets tab bartitle.

self.tabBarItem.title = @"my title";设置标签栏标题。

self.title = @"my title";sets both of these.

self.title = @"my title";设置这两个

回答by Suragch

Swift

迅速

Set top bar title

设置顶部栏标题

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

Set tab item title

设置标签项标题

self.tabBarController?.tabBar.items?[0].title = "tab title"

Set both titles

设置两个标题

self.title = "both titles"

回答by Zaid Pathan

For Swiftuse this,

对于Swift使用这个,

self.navigationItem.title = "Navigation bar title" 
self.title = "Tab bar title"

回答by Pavan

Note: If you have a tab bar controller with navigation controllers at the root of each view controller, setting the tab bar item on the view controllers won't affect the title if you're setting the navigationItem.title. You'll need to set the tabBarItemonto the navigation controller instead for it to be picked up from the tab bar controller.

注意:如果您在每个视图控制器的根目录有一个带有导航控制器的标签栏控制器,如果您设置了navigationItem.title. 您需要将 设置tabBarItem到导航控制器上,以便从标签栏控制器中获取它。

None of the answers posted by others worked for me because my tab bar's view controllers all have navigation controllers at their root - this is a common hierarchy pattern for UITabBarController. You have to set the navigation controller's tabBarIteminstead to get the title to show differently from the navigationItem's title

其他人发布的答案都没有对我有用,因为我的选项卡栏的视图控制器都在其根目录下有导航控制器 - 这是UITabBarController. 你必须设置导航控制器tabBarItem,而不是拿到冠军,从不同显示navigationItem的标题

You can create your tabBarItemand associate them to your VC directly like so.

您可以tabBarItem像这样直接创建您的 VC 并将它们关联到您的 VC。

    let tabBarVCOne = BooksListViewController()
    tabBarVCOne.tabBarItem = UITabBarItem(title: "Books", image: nil, tag: 0)

    tabBarViewControllers.append(tabBarVCOne)
    ...

Then you'll have something like this:

然后你会有这样的事情:

    //Wrap each view controller in a navigation controller. 
    self.viewControllers = tabBarViewControllers.map(UINavigationController.init)

But that should be changed to the following in order to grab the already associated tabBarItemfrom the view controller and set it onto the navigation controller automatically.

但这应该更改为以下内容,以便tabBarItem从视图控制器中获取已关联的内容并将其自动设置到导航控制器上。

    self.viewControllers = tabBarViewControllers.map({
        let navigationController = UINavigationController(rootViewController: 
import UIKit

extension UIViewController {

/// Setting the navigation title and tab bar title
///
/// - Parameters:
///   - navigationTitle: Navigation title
///   - tabBarTitle: TabBar title
func setTitles(navigationTitle: String, tabBarTitle: String) {
    // Order is important here!
    title = tabBarTitle
    navigationItem.title = navigationTitle
 }

}
) navigationController.tabBarItem =
override func viewDidLoad() {
    super.viewDidLoad()
    setTitles(navigationTitle: "Login", tabBarTitle: "Home")
}
.tabBarItem return navigationController })

You will now be able to have a different title (set from your VC) separate from the title defined for your tabBarItem.

您现在可以拥有一个不同的标题(从您的 VC 设置)与为tabBarItem.

回答by cs4alhaider

Swift 4.2

斯威夫特 4.2

Here you go, I created an extension for UIViewController:

好了,我为 UIViewController 创建了一个扩展:

@interface YourTabBarController : UITabBarController <UITabBarControllerDelegate, UINavigationControllerDelegate>

@end

And then from your controller:

然后从您的控制器:

- (void) viewDidLoad {
    // UITabBarControllerDelegate
    self.delegate = self;

    // UINavigationControllerDelegates
    yourNavigationController.delegate = self;
    ...
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    yourNavigationController.tabBarItem.title = @"Tab Bar Title";
    ...
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    yourNavigationController.tabBarItem.title = @"Tab Bar Title";
    ...
}

回答by wolffan

Probably a bit late (but).

可能有点晚(但是)。

Setting the title of a VC changes the title of the Navigation AND the tabBar. (if the VC is already attached to both).

设置 VC 的标题会更改 Navigation 和 tabBar 的标题。(如果 VC 已经连接到两者)。

If you want to have separate titles, you need to manually set those, you normally set the title for the VC and then specifically the title of the tabBarItem, since it's a property of the

如果你想有单独的标题,你需要手动设置这些,你通常为 VC 设置标题,然后专门设置 tabBarItem 的标题,因为它是

回答by Ruiz

Pretty late to this. You could have your TabBarController serve as the UITabBarControllerDelegate and UINavigationControllerDelegate for itself and the navigation controllers embedded in each of your tabs respectively.

到这里已经很晚了。您可以让 TabBarController 作为自己的 UITabBarControllerDelegate 和 UINavigationControllerDelegate 以及嵌入在每个选项卡中的导航控制器。

.h:

。H:

##代码##

.m:

.m:

##代码##

Based on some quick testing, it seems like these two delegate actions should cover any loose cases and will update the title whether you're switching tabs or browsing in your navigation controller. For completeness, you could update your title in didShowViewControlleras well, but based on what I've seen, I don't think it's necessary.

根据一些快速测试,这两个委托操作似乎应该涵盖任何松散的情况,并且无论您是切换选项卡还是在导航控制器中浏览,都会更新标题。为了完整起见,您也可以在didShowViewController 中更新您的标题,但根据我所看到的,我认为没有必要。