3

我已经以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;
}
4

2 回答 2

8

iOS7 中的新功能,现在执行此操作的方法是使用selectedImageon 属性tabBarItem

self.tabBarItem.selectedImage = [UIImage imageNamed:@"Free_Selected.png"];
于 2014-02-21T02:44:05.933 回答
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
于 2012-05-22T12:34:39.100 回答