Patrick
Patrick

Reputation: 7185

XCode: Remove Selection Indicator Image from a particular tab

I am using a custom tab bar image and the middle tab is also a custom image (much like an older version of Instagram).

Here is some of my code:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;

    [tabBar setBackgroundImage:[UIImage imageNamed:@"CustomTabBar.png"]];

    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
    tabBarItem3.title = nil;
    tabBarItem3.image = nil;
    [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"tab-button-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-button.png"]];

This works great, but I have one issue I cannot fix. The selected tab has, by default, a light grey background. This is an effect which I would like to keep, but not for the middle tab. The middle tab is a larger round image which does change when it is selected, but still the grey background appears.

Is there a way to remove this like [tabBar setSelectionIndicatorImage:[[UIImage alloc] init]]; but for that tab only. Or, in the app delegate, detect a change in tab and remove it there?

Upvotes: 0

Views: 3169

Answers (2)

Biggs
Biggs

Reputation: 267

I've tried implementing this in my code. However I made the original tab bar in storyboards and it doesn't seem to be overridden when I add a tab bar in my app delegate. I was wondering how I'd go around doing it?

I can override the tab bar items like so

//create new tab bar appearance
UITabBar *tabBar = [UITabBar appearance];
//set background image
[tabBar setBackgroundImage:[UIImage imageNamed:@"menu.png"]];
//create a colour and apply it as tint colour when items aren't selected.
UIColor *color=[UIColor colorWithRed:64.0/255.0 green:147.0/255.0 blue:52.0/255.0 alpha:255.0];
UIColor *colorSelected=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
[tabBar setTintColor:color];
[tabBar setSelectedImageTintColor:colorSelected];

[tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selection-tab.png"]];

All of that is just in the didfinishloadingwithoptions

Upvotes: 0

Patrick
Patrick

Reputation: 7185

Ok, so it turns out a nice lunch time walk really helps in these situations. Here is my answer for anyone else who has a similar problem.

I first include <UITabBarControllerDelegate> in the .h of my app delegate. And in the didFinishLaunchingWithOptions method I set the delegate of the tab bar:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;

Then I can use this method to toggle whether or not to show the background image or not:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    UITabBar *tabBar = tabBarController.tabBar;
    if (tabBar.selectedItem == [tabBar.items objectAtIndex:2]) {
        [tabBar setSelectionIndicatorImage:[[UIImage alloc] init]];
    }
    else {
        [tabBar setSelectionIndicatorImage:nil];
    }
}

Upvotes: 6

Related Questions