Question

My objective is to make an if/else statement so that I can say: if a TabBarItem is selected, setSelectedImageTintColor to this color.

I am new to ObjC and not exactly sure how to implement the if statement. Here is my viewDidLoad:

- (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:
 [UIImage imageNamed:@"item.png"]];


}

As you can see I have a separate image specified for each of the TabBarItems, and I would like to make the highlight color match the image (so a red highlight for red.png, a yellow for yellow.png, etc)

How can I implement this if/else statement? Again, I'd like to check for the indexPath (0-3) and then set a custom setSelectedImageTintColor for the tabBarItem. Another option for me would be to remove the highlight altogether, if this would be more practical.

Was it helpful?

Solution 2

You should implement the UITabBarDelegate protocol into your ViewController, and set the UITabBar delegate to self

self.tabBar.delegate = self;

After that, you can implement the method tabBar:didSelectItem:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    // Check here for the item and change tintColor accordingly
    // For example:
    if([item isEqual:[self.tabBar.items objectAtIndex:1]) {
        tabBar.selectedImageTintColor = [UIColor redColor];
    }
}

OTHER TIPS

- (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];
}

I'm not 100% certain of what you're trying to do up above (would have been nice to have included a screenshot), but if you want to know which UITabBar item is selected, you should set a delegate on your UITabBarController and then write a method that implements:

-tabBarController:didSelectViewController:

And then you can change the color or icon of your tab bar item.

If you set your tab bar controller to an IBOutlet, you can programatically ask it which tab is selected by just looking at the controller's "selectedIndex" property.

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