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

Fra*_*ank 8 iphone uitabbaritem uitabbar

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 http://www.freeimagehosting.net/uploads/11a2137011.png

Fra*_*ank 11

刚刚找到我的解决方案 基本上,我将UITabItem子类化并在导航控制器中设置它:

-(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;            
}
Run Code Online (Sandbox Code Playgroud)

以下是CustomTabBarItem类的外观:

@interface CustomTabBarItem : UITabBarItem
{
    UIImage  *customHighlightedImage;
}

@property (nonatomic, retain) UIImage *customHighlightedImage;

@end
Run Code Online (Sandbox Code Playgroud)

执行:

#import "CustomTabBarItem.h

@implementation CustomTabBarItem

@synthesize customHighlightedImage;

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

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

@end
Run Code Online (Sandbox Code Playgroud)


Muh*_*wan 6

在iOS 6中,我更改了选定的tabbatitem图像,如 -

在tabbar控制器委托方法中

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

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

通过这个你可以改变你的形象.

或者你可以直接在你的视图控制器init(或ViewWillAppear)方法中使用

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
Run Code Online (Sandbox Code Playgroud)

我希望这对你有帮助.