4

I'm trying to change the name of the tab bar item in my app programmatically.

In my storyboard I have a UITabBarController set as the initial view and the hierarchy is as follows:

UITabBarController -> UINavigationController -> UITableViewController -> Detail View Controller

To change the title of the bar item you need to change the bar item's title of the UINavigationController. In IB everything works well, but I need to localize my app, which I do dynamically (without app restart) and every time I start up the app the titles of the ones given in IB is set and the titles won't change to their localized title until I tap the bar item.

In my respective UITableViewControllers I set the title with self.title = localized title; and that works well. But I want to have the app change the bar item titles at start up, and not until I tap them.

I've looked at posts here on SO regarding this issue and tried the suggestions, but still the titles of the bar items are always set to their values from IB. I also tried this code below, but that only sets the selected bar item at startup:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Select the desired tab of our initial tab bar controller:
    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
    tabBar.selectedIndex = 1;
    tabBar.navigationController.title = @"Item 2";

    [(UITabBarItem*)[self->tabBarController.tabBar.items objectAtIndex:1] setTitle:@"Item 2"];

    // Override point for customization after application launch.
    return YES;

}

4 Answers4

6

Ok so I'm answering my own question. After quite a long time searching the web, SO, and trying all sorts of combinations I cracked it. The post below helped me get on track, but it didn't solve it (still gave it +1):

Tab Bar Item title before appear Storyboard

In the UITableViewController .m file you need to add this code to set the title of a tab bar item before it is loaded, or tapped.

- (id)initWithCoder:(NSCoder *)decoder {

    self = [super initWithCoder:decoder];
    if (self) {
        self.navigationController.tabBarItem.title = NSLocalizedStringFromTableInBundle(@"TITLE_TABLANGUAGE", nil, currentLanguageBundle, @"");
    }
    return self;
}

The answer lies in that the UITabBarController's bar items get their values, in this case a title, from the UINavigationController's bar items' titles. And it's important to set that title in the initWithCoder method. Putting it else will not set the title before it's tapped.

Community
  • 1
  • 1
4

I think you are making it more difficult than it needs to be. To change the title of a tab bar item at runtime, simply do the following from the viewcontroller that maintains the tab in question:

self.navigationController.tabBarItem.title = @"desired title text";

done.

TNBtech
  • 492
  • 4
  • 7
1

Use this function directly:

     private func updateTabBarTitles() {
     if let navControllers = viewControllers as? [UINavigationController] {

         let vcs = navControllers.map({$0.viewControllers.first!})

         for vc in vcs {
             switch vc {
           case is First:    vc.navigationController?.tabBarItem.title = "firstVCTabbarTitle"
           case is Second:    vc.navigationController?.tabBarItem.title = "secondVCTabbarTitle"
           default: break

             }
         }
     }
 }
Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
0

In Swift 5 and Xcode 11

A great option is to use the SF Symbols. Download SF Symbols from the Apple website. Then open the app and select a symbol you like. Copy the name with CMD-Shift-C.

Use it on your View Controller

profileViewController.tabBarItem = 
   UITabBarItem(
      title: "Profile", 
      image: UIImage(systemName: "person.fill"),
      tag: 2)
multitudes
  • 2,898
  • 2
  • 22
  • 29