Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically setting UITabBarItem icons with StoryBoards?

Tags:

xcode

iphone

ios5

I want to use a UITabBarSystemItem for the icon of one of my tabBarItem's but I'm using storyboards. I'm not sure where to set it. If I set it in the view's viewDidLoad then it doesn't change until you push the button in the tabBar. Before that it's just the blue ? square.

And as far as I know, you can't use UITabBarSystemItems in IB inspector.

UPDATE:

Well first of all I'm an idiot. You can totally choose the icon in the IB Inspector.

Tab Bar Item -> Identifier -> Choose your icon.

But the question still remains how to do it programmatically. Or rather when/where?

like image 873
Will Larche Avatar asked Nov 29 '22 17:11

Will Larche


2 Answers

When using storyboards, a view controller initializes using the initWithCoder constructor so you could override that function and set the system item icon there like so

- (id)initWithCoder:(NSCoder*)aDecoder 
{
    if(self = [super initWithCoder:aDecoder]) 
    {
        self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
    }
    return self;
}

Naturally, you can change the value of the system item to any of the values supported. Apple lists them here

like image 79
John F Avatar answered Dec 01 '22 08:12

John F


Check this code for your problem: Its working in my app.

- (NSArray *) initializeViewControllers
{
    NSArray *viewControllerArray = nil;
    viewController1 = <View Init Code>
    viewController2 = <View Init Code>
    viewController3 = <View Init Code>

    1stNavController = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
    UIImage *img = [UIImage imageNamed:@"tab_home"];
    [1stNavController .tabBarItem initWithTitle:@"Home" image:img tag:1];

    2ndNavController = [[UINavigationController alloc] initWithRootViewController:viewController2]; 
    img = [UIImage imageNamed:@"tab_timeDrop"];
    [2ndNavController .tabBarItem initWithTitle:@"Time Entry" image:img tag:2];

    3rdNavController = [[UINavigationController alloc] initWithRootViewController:viewController3];

    img = [UIImage imageNamed:@"tab_invoiceSummary"];
    [3rdNavController.tabBarItem initWithTitle:@"Invoice Summary" image:img tag:3];

    viewControllerArray = [NSArray arrayWithObjects:1stNavController,2ndEntryNavController,3rdReportNavController, nil];
    return viewControllerArray;
}

This code is returning View Controllers with Images for their respective tabs. Here i used Navigation Controller inside the tabbar controller. you can also use view controller instead of navigation Controller.

Just add this code and initialize your tabbar controller as follows inside appdidfinishlaunching method:

 tabbarController = [[UITabBarController alloc] init];

_tabbarController.viewControllers = [self initializeViewControllers];

self.window.rootViewController = tabbarController;

Hope it works.

Please reply.

like image 22
2 revs, 2 users 94% Avatar answered Dec 01 '22 08:12

2 revs, 2 users 94%