如何在iOS中更改标签栏图标的颜色

7

我的当前选项卡栏如下所示:

enter image description here

我的代码如下:

-(void)startTabBar{
     self.tabBarController = [[UITabBarController alloc] init];
     TAB_1  *tab_1 = [[TAB_1 alloc]init];
     TAB_2  *tab_2 = [[TAB_2 alloc]init];
     TAB_3  *tab_3 = [[TAB_3 alloc]init];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary  dictionaryWithObjectsAndKeys: [UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
   [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];

    NSArray* controllers = [NSArray arrayWithObjects:tab_1,tab_2, tab_3, nil];

   self.tabBarController.viewControllers = controllers;
   self.window.rootViewController = self.tabBarController;
}

我想要做的是:
普通选项卡:选项卡的标题应该像黑色一样,但只有图标图像应该是黑色。期望的选项卡应该是这样的:
选中选项卡:选项卡的标题应该像红色一样,但只有图标图像应该是红色。期望的选项卡应该是这样的:
选项卡栏颜色:使整个选项卡栏颜色更透明,颜色相同。
有人可以帮忙吗?

请查看此答案:http://stackoverflow.com/a/18742880/1679187 - Yogesh Suthar
@YogeshSuthar 那个答案已经过时了。 - michaelsnowden
3个回答

22
这可以实现你所要求的:
[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
[[UITabBar appearance] setAlpha:0.25];

是的,你说得对,它将选项卡栏设置为透明。此外,我不得不在每个选项卡类的initwithinib方法中取消注释//[[self tabBarItem] setImage:[[UIImage imageNamed:@"icon_feed.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; - Lasang

7
在iOS8上使用Swift,代码如下:

UITabBar.appearance().tintColor = UIColor.redColor()

2
这里的答案并不完全符合我的期望。如果你想要改变应用程序中所有选项卡控制器的通用颜色,那么这是有道理的,但实际上,你不一定想要做出如此全局的更改(更不用说后期调试和查找可能会很困难)。最好是更加专注,直接改变颜色。
从iOS 8开始,您需要更改选项卡栏的tintColor属性。希望您正在子类化UITabBarController。如果是这样,您可以在viewDidLoad中设置颜色:
- (void)viewDidLoad {
    [super viewDidLoad];

    self.tabBar.tintColor = [UIColor grayColor];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接