我已经以编程方式定义了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;
}
答案 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