Shishir.bobby
Shishir.bobby

Reputation: 10984

Set title of tab bars programmatically

I have five tabs in my main window, but I haven't created it programmatically.

What should I do to create the title through code?

Upvotes: 5

Views: 5126

Answers (4)

dustinrwh
dustinrwh

Reputation: 926

If your UIViewController is wrapped in a UINavigationController, do the following:

self.navigationController?.tabBarItem.title = "Awesome Title"

Upvotes: 0

Madhup Singh Yadav
Madhup Singh Yadav

Reputation: 8124

Hey do you want to set the title of the tab bar button or the viewcontroller that is associated with the tab?

If you are looking for the first thing then do it like:

UITabBarItem *tabItem = [[[tabBarController tabBar] items] objectAtIndex:yourIndex];
[tabItem setTitle:@"theTitle"];

otherwise mihirpmehta is correct.

Hope this helps.

Upvotes: 12

Mihir Mehta
Mihir Mehta

Reputation: 13843

OR

[(UIViewController *)[tabBarController.viewControllers objectAtIndex:1] setTitle:@"Title1"];

Upvotes: 5

Chintan Patel
Chintan Patel

Reputation: 3165

You can initialize and use a UITabBarItem object using the following functions:

- (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag; // selected image autogenerated
- (id)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag

And then you can call this method on your tabBar object:

- (void)setItems:(NSArray *)items animated:(BOOL)animated;

where items id the array to which you have added all your tabBarItem objects. There is also a badgeValue property for UITabBarItem if you need to use.

Upvotes: 0

Related Questions