4

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?

Will Larche
  • 3,119
  • 7
  • 26
  • 34

4 Answers4

11

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

woz
  • 10,888
  • 3
  • 34
  • 64
John F
  • 865
  • 1
  • 8
  • 15
7

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.

Jack
  • 10,943
  • 13
  • 50
  • 65
iCreative
  • 1,499
  • 1
  • 9
  • 22
  • 1
    This code is right, *IF* you're creating the ViewControllers and pushing them yourself. But the question is about Storyboards, so this answer is unrelated to the question. – Diego Freniche Apr 04 '12 at 15:34
3

Well that's quite simple!

For the where -> create a custom TabBarController (e.g. WBTabBarController). In your StoryBoard explicitly set it.

For the how -> just overwrite viewDidLoad of the WBTabBarController and set your images for the TabBarItems. In the following example this was done using the NIKFontAwesomeIconFactory:

#import "WBTabBarController.h"
#import "NIKFontAwesomeIcon.h"
#import "NIKFontAwesomeIconFactory.h"
#import "NIKFontAwesomeIconFactory+iOS.h"

@interface WBTabBarController ()

@end

@implementation WBTabBarController


- (void)viewDidLoad
{
    [super viewDidLoad];

    NIKFontAwesomeIconFactory *factory = [NIKFontAwesomeIconFactory tabBarItemIconFactory];

    UITabBarItem *item0 = self.tabBar.items[0];
    item0.image = [factory createImageForIcon:NIKFontAwesomeIconApple];

    UITabBarItem *item1 = self.tabBar.items[1];
    item1.image = [factory createImageForIcon:NIKFontAwesomeIconStackOverflow];
}

@end
s.muellner
  • 61
  • 4
2

Assuming the initial scene of your storyboard is the tab bar view controller, you can access it in applicationDidFinishLaunching, it is the rootViewController of the app delegate's window. I don't know if you can swap in a system item if the bar item is already present, but you can definitely set the image and title.

jrturton
  • 118,105
  • 32
  • 252
  • 268