timofey.tatarinov
timofey.tatarinov

Reputation: 127

How to change default title of the UITabBarItem

I want to use standard tab bar item with custom title. I change title after TabBarItem creating directly self.tabBarItem.title = @"Liked". For example "Favorites" - UITabBarSystemItemFavorites:

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Liked", @"Liked");
        self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
        self.tabBarItem.title = @"Liked";
    }
    return self;
}

Can I do this? If yes, where I made mistake?

UPD: Changing tab bar item title in the viewDidLoad() works on iOS4 but doesn't work on iOS5. Does exist another approach?

Thanks

Upvotes: 0

Views: 1414

Answers (2)

multitudes
multitudes

Reputation: 3545

As per Swift5.1 and Xcode the title and image of the system tab bar items cannot be changed. They are defined as enum in obj c as follow. This is the list:

typedef enum UITabBarSystemItem : NSInteger {
case more, favorites, featured, topRated, recents, contacts, history, bookmarks, search, downloads ,mostRecent, mostViewed
} UITabBarSystemItem;

This has a reason. Some icons are immediately recognisable across the whole iOS ecosystem and mean the same thing to everyone, therefore it is not possible to use them differently. In this way Apple creates a better user experience. You cannot use the Apple icon and use a different title. You could use a custom icon and custom title instead or in the Storyboards select an SF Symbol like this:

enter image description here

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25927

You should do that in the viewDidLoad and not in the init.

Upvotes: 2

Related Questions