Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

iphone - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...