- (void)viewDidLoad
{
[super viewDidLoad];
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
item0.image = [[UIImage imageNamed:@"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
item1.image = [[UIImage imageNamed:@"yellow.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
item2.image = [[UIImage imageNamed:@"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *item3 = [self.tabBar.items objectAtIndex:3];
item3.image = [[UIImage imageNamed:@"black.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[[UITabBar appearance] setSelectionIndicatorImage:
self.tabBar.selectedItem.image];
}
If you want the image to update whenever you switch tabs:
In your .h file, you will have a line like this:
@interface MyClass : UIViewController
Change it to
@interface MyClass : UIViewController <UITabBarDelegate>
replacing MyClass with the name of your class, of course.
In your viewDidLoad:
, add this line:
self.tabBar.delegate = self;
In your .m file, implement tabBar:didSelectItem:
like this:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
[[UITabBar appearance] setSelectionIndicatorImage:item.image];
}