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

时间:2014-07-02 08:55:29

标签: ios objective-c uitabbarcontroller uitabbar

我当前的标签栏如下所示:

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;
}

我想做的是:

普通标签: 标签的标题应该是黑色的,但只有图标图像应该是黑色。预期的标签应该是:

enter image description here

选定标签: 标签标题应为红色,但只有图标图片应为红色。预期标签应为:

enter image description here

标签栏颜色使整个tabBar颜色更透明,颜色相同

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:22)

这可以满足您的要求:

[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
[[UITabBar appearance] setAlpha:0.25];

答案 1 :(得分:2)

这里的答案并不是我想要的。如果您想要对应用程序中所有标签栏控制器的颜色进行一般性更改,这是有道理的,但实际上,您不一定要进行这样的全局更改(更不用说以后调试和查找很难)。最好是更专注,所以你想直接改变颜色。

iOS 8 开始,您需要更改标签栏的tintColor属性。希望你是你的UITabBarController的子类。如果是,您可以在viewDidLoad

中设置颜色
- (void)viewDidLoad {
    [super viewDidLoad];

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