Question

I have a TabBar that I've created through IB, I chose "create new project" -> "Tab bar application". Is there a way for me to access one of the TabBarItems for customization through the code?

It seems to me that something like: [[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:@"Button one"]; should set the title of that item to "Button one", but it doesen't. The title itself is not a problem (I can set that through IB aswell), however adding an Icon seems to be.

So to sum up, what I really want to know is: Is there a way to add an Icon to a TabBarItem created through IB?

SOLUTION: Adding in viewDidLoad in the first view, being loaded automatically upon starting the app:

UITabBarController *tb = [self tabBarController];
[[tb.tabBar.items objectAtIndex:1] setTitle:@"Title"];

Let me set the title of the second button (objectAtIndex: 1). I was also able to set the image the same way, which also worked for buttons one (objectAtIndex: 0) and three (objectAtIndex: 2).

Was it helpful?

Solution

Add this to your viewDidLoad: method of one of the tabBar viewControllers and it should work:

- (void)viewDidLoad
{
    [super viewDidLoad];


    //Get the tabBarItem
    UITabBarItem *tbi = [self tabBarItem];

    //Give it a lable
    [tbi setTitle:@"Title A"];

    //create a image from a file for the tabBar
    UIImage *i = [UIImage imageNamed:@"NiceImage.png"];
    //and put it on the tabBar
    [tbi setImage:i];

}

OTHER TIPS

You should be able to set the image and title properties on the TabBarItems:

UITabBarItem *item = (UITabBarItem *)[tabBarController.tabBar.items objectAtIndex:0];
item.image = [UIImage imageNamed:@"home.png"];

Don't forget that the UITabBar only uses the alpha values out of the image you set, so if you don't have an alpha channel in the image you may not see anything when you set an image on the tab bar item.

I've never created a tab bar through IB (always through code), however to set title and icon I use

controller.title = @"Controller";
controller.tabBarItem.image = [UIImage imageNamed:@"image.png"];

where controller is the UIViewController added to the viewControllers' array of the UITabBarController.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top