ChatGPT解决这个技术问题 Extra ChatGPT

how to change uiviewcontroller title independent of tabbar item title

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 uiviewcontroller change to: my title

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?


M
MobileMon

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"

Make sure, if you are still using self.title to set your uitabbar title, to set self.navigation setTitle after otherwise it won't work
in xcode 9 and ios 11 in swift 4 it show error that unambiguous use of title
Working for me in Xcode 10.2 using Swift 4.2, thanks a ton!
S
Simon Epskamp

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

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

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

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


C
Community

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"

What is topItem?
This was the only way I could set my top bar title in my configuration, not sure why "self.navigationItem.title" didn't work for me.
M
Mohammad Zaid Pathan

For Swift use this,

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

in xcode 9 and ios 11 in swift 4 (self.navigation.title) showing error that unambiguous use of title
P
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 tabBarItem onto the navigation controller instead for it to be picked up from the tab bar controller.

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 tabBarItem instead to get the title to show differently from the navigationItem's title

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

    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 tabBarItem from the view controller and set it onto the navigation controller automatically.

    self.viewControllers = tabBarViewControllers.map({
        let navigationController = UINavigationController(rootViewController: $0)
        navigationController.tabBarItem = $0.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.


C
Community

Swift 4.2

Here you go, I created an extension for UIViewController:

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
 }

}

And then from your controller:

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

R
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.

.h:

@interface YourTabBarController : UITabBarController <UITabBarControllerDelegate, UINavigationControllerDelegate>

@end

.m:

- (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";
    ...
}

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 didShowViewController as well, but based on what I've seen, I don't think it's necessary.


w
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).

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


B
Burcu Kutluay

Setting navigation title is the correct answer but when it did not work for me because i only used tabbarcontroller and controllers realted to that. First of all I needed to add navigation controller for view controllers then it works perfectly.

   override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Navigation bar title"
}

https://i.stack.imgur.com/i1RCb.png