Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use a custom selected image for UITabBarItem?

I like to have a custom selected image when a user selects an item on the tab bar, by default it selects as blue like but would like to have a green color instead. something like below any thoughts?

alt text

like image 729
Frank Avatar asked Aug 11 '10 19:08

Frank


People also ask

How do I create a custom tab bar in Swift?

To add a Tab Bar Controller to the Storyboard, select the 4 placeholders that we just created and the View Controller. Then, go to Editor, select Embed in and Tab Bar Controller. This will envelop all those scenes in a single Tab Bar Controller. Put the new Tab Bar Controller on top of the other controllers.

How do I change the background color of a tab bar in Swift?

backgroundColor = UIColor(red:1, green:0, blue:0, alpha:1) / UITabBar. appearance(). tintColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1) // New!! func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {...}


2 Answers

Just found my solution. Basically, I subclassed UITabItem and set this in the navigation controller:

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"Events" image:[UIImage imageNamed:@"tabIcon.png"] tag:0];
    tabItem.customHighlightedImage=[UIImage imageNamed:@"tabIconSelected.png"];
    self.tabBarItem = tabItem;
    [tabItem release];
    tabItem=nil;            
}

Here's what the CustomTabBarItem class looks like:

@interface CustomTabBarItem : UITabBarItem
{
    UIImage  *customHighlightedImage;
}

@property (nonatomic, retain) UIImage *customHighlightedImage;

@end

implementation:

#import "CustomTabBarItem.h

@implementation CustomTabBarItem

@synthesize customHighlightedImage;

- (void)dealloc {
    [customHighlightedImage release];
    customHighlightedImage=nil;
    [super dealloc];
}

-(UIImage *)selectedImage {
    return self.customHighlightedImage;
}

@end
like image 60
Frank Avatar answered Oct 21 '22 06:10

Frank


In iOS 6 I have change the selected tabbatitem image like -

in tabbar controller delegate method

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

{
    if([tabBarController selectedIndex] == 0)
    {
        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
    }    
}

through this you can change your image.

Or you can use directly in your view controllers init(or ViewWillAppear) method, like

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];

i hope this'll help you.

like image 23
Muhammad Rizwan Avatar answered Oct 21 '22 07:10

Muhammad Rizwan