简体   繁体   English

如何在iOS中更改TabBar图标颜色

[英]how to change tabBar icon color in ios

My current tab bar looks as follows: 我当前的标签栏如下所示:

在此处输入图片说明

My code is as follows: 我的代码如下:

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

What i want to do is: 我想做的是:

Normal tab: title of tab should be black as it is but only icon image should be black. 普通标签:标签 标题应为黑色,但仅图标图像应为黑色。 Expected tab should be like : 预期标签应为:

在此处输入图片说明

Selected tab: title of tab should be red as it is but only icon image should be red. 选定的标签:标签 标题应为红色,但仅图标图像应为红色。 Expected tab should be like : 预期标签应为:

在此处输入图片说明

tab bar color : make the whole tabBar color more transparent with same color 标签栏颜色使用相同的颜色使整个tabBar颜色更透明

Can anyone help to do this? 有人可以帮忙吗?

This accomplishes what you're asking for: 这可以满足您的要求:

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

在iOS8上的Swift中,它将是:

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

The answers here aren't quite what I was looking for. 这里的答案并不完全是我想要的。 It makes sense if you want a generic change to the color of all tab bar controllers in your app, but realistically, you don't necessarily want to make such a global change (not to mention that it can be difficult to debug and find later). 如果您想对应用程序中所有选项卡栏控制器的颜色进行通用更改,这是有道理的,但实际上,您不一定要进行这样的全局更改(更不用说以后很难调试和查找) )。 It's better to be more focused, so you want to change the color directly. 最好集中注意力,以便您直接更改颜色。

As of iOS 8 , you need to change the tintColor property of the tab bar. iOS 8开始 ,您需要更改选项卡栏的tintColor属性。 Hopefully, you're subclassing your UITabBarController . 希望您正在将UITabBarController子类化。 If you are, you can set the color in viewDidLoad : 如果是这样,可以在viewDidLoad设置颜色:

- (void)viewDidLoad {
    [super viewDidLoad];

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM