为UITabBar设置活动图像

时间:2012-05-22 12:15:13

标签: objective-c xcode ios5

我已经以编程方式定义了UITabBar但如何在项目处于活动状态时设置自定义UITabBar图标?

以下是我用于声明UITabBar按钮的代码,但如何在自定义图片处于活动状态时设置它?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        self.title = NSLocalizedString(@"Free", @"Free");
        self.tabBarItem.image = [UIImage imageNamed:@"Free.png"];
    }

    return self;
}

2 个答案:

答案 0 :(得分:7)

iOS7中的新功能,现在的方法是使用selectedImage上的tabBarItem属性:

self.tabBarItem.selectedImage = [UIImage imageNamed:@"Free_Selected.png"];

答案 1 :(得分:2)

您需要为每个标签栏项目设置正常和选定状态:

UIImage *normalImg = [UIImage imageNamed:@"Free.png"];
UIImage *selectedImg = [UIImage imageNamed:@"Free_Selected.png"];

[self.tabBarItem setBackgroundImage:normalImg forState:UIControlStateNormal]; //Normal
[self.tabBarItem setBackgroundImage:selectedImg forState:UIControlStateSelected]; //Selected