7

I just wanted to know how i can set a title of a tab bar item using UITabBarSystemItem ?

What i did :

self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];

So to change the title by default instead of "Featured" (because of UITabBarSystemItemFeatured object), I wrote :

self.tabBarItem.title = @"Actu";

So in my mind i should have "Actu" as title instead of "Featured".

But it changes nothing, the title keeps being "Featured" (default title).

I also tried :

[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(@"Actu", @"Actu")];

(because this tabbaritem is at index 0), but nothing changes.

Or maybe such a modification is not possible using UITabBarSystemItem objects ?

I hope this is well enough explained :/

PS : Sorry for my english and anything else wrong, 1st post ever… :/

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
Lucien
  • 451
  • 4
  • 16

5 Answers5

4

When a UITabBarItem is initalized using initWithTabBarSystemItem:tag: you cannot change the image or title properties later on.

Source: iOS Development Documentation

Leon Lucardie
  • 9,541
  • 4
  • 50
  • 70
  • Ok, so I have to import my own picture if I want to set the title. Thanks for the fast answer ! – Lucien Apr 25 '13 at 09:16
3

You can use KVC.

[self.tabBarItem.setValue("YourTitle", forKey: "internalTitle")];

Heisenbean
  • 61
  • 6
1

You already wrote that:

… such a modification is not possible using UITabBarSystemItem objects…

Those have title and image properties set to nil, so they have these values stored in some internal private properties.

Also the docs say:

-initWithTabBarSystemItem:tag:
The title and image properties of the returned item cannot be changed later.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
1

Actually it is possible, you can utilize method _setInternalTitle: from private API.

[self.tabBarItem setValue:@"Categories" forKey:@"internalTitle"];

Precaution: use it only for test builds.

Roman B.
  • 3,598
  • 1
  • 25
  • 21
  • Awesome, this works. This is handy for using the system icons when rapid prototyping. Here's the code in Swift (works in Xcode 10 iOS 12): navigationController.tabBarItem.setValue("Hello", forKey: "internalTitle") – Adam Waite Dec 06 '18 at 22:18
-3

I realize this is old, but perhaps this would help somebody else that wants to do this. Create a tabBarItem from the system item as indicated above. Then create another tabBarItem as if you had a custom icon and copy the image from the system item.

        UITabBarItem* systemItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:theNameIWantToUse image:systemItem.image tag:0];
Philip Nelson
  • 985
  • 6
  • 20