如何在iOS 7中更改tabBarItems的文本和图标颜色?

Ed *_*dez 27 tabbar uitabbarcontroller uitabbaritem uitabbar ios7

如何在iOS 7中更改UITabBar和UITabBarItems的文本和图标颜色?对于未选中的tabbar项,默认的灰色文本看起来很暗,难以阅读.

Ed *_*dez 69

你需要做两件事:

1)如果要自定义TabBar本身,则需要为tabBarController设置barTintColor:

    // this will generate a black tab bar
    tabBarController.tabBar.barTintColor = [UIColor blackColor];

    // this will give selected icons and text your apps tint color
    tabBarController.tabBar.tintColor = appTintColor;  // appTintColor is a UIColor *
Run Code Online (Sandbox Code Playgroud)

2)为要覆盖的每个状态设置tabBarItem文本外观:

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                    NSForegroundColorAttributeName : appTintColor
                                                    } forState:UIControlStateSelected];


// doing this results in an easier to read unselected state then the default iOS 7 one
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                    NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
                                                    } forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

  • 如何设置未选择的图标颜色? (5认同)
  • 但它没有给标签栏提供黑色..它显得有点灰色.. (2认同)

atr*_*rik 43

这对我来说很有用,可以在标签栏中显示不活动的项目

UITabBarItem *item = [self.tabBar.items objectAtIndex:1];
Run Code Online (Sandbox Code Playgroud)

//这里你需要使用你想要的颜色的图标,因为它将按原样呈现

item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Run Code Online (Sandbox Code Playgroud)

//此图标用于选定的标签,它将按照中的定义进行着色

self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected.png"];
Run Code Online (Sandbox Code Playgroud)

  • 使用“imageWithRenderingMode”是更改选项卡图像颜色的关键 (2认同)

Tak*_*ori 11

Ed的答案很完美,但我要补充一点.TabBar默认为半透明,因此受TabBar下的视图颜色的影响(即每个成员viewController的视图的颜色会影响TabBar外观.).

所以我在下面设置代码不受影响.

self.tabBarController.tabBar.translucent = false;
Run Code Online (Sandbox Code Playgroud)

与Ed的答案一起在这里是我现在使用的完整代码.

self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
self.tabBarController.tabBar.translucent = false;
self.tabBarController.tabBar.tintColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)


Kap*_*ppe 7

在iOS 8中测试永久文本颜色(选定/未选定)和图像颜色(选定/未选定),而不创建两个具有不同颜色的图像foreach选项卡:

文字颜色:

[[UITabBar appearance] setTintColor: selectedTabColor ];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       **yourFont**, NSFontAttributeName,
                                                       ** selectedTabColor**, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       **yourFont**, NSFontAttributeName,
                                                       **selectedTabColor**, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)

图像颜色:(假设原始图像具有您想要显示为未选择的颜色)

UITabBarController子类-awakeFromNib中:

    for (int i =0; i<self.viewControllers.count; i++)
    {
        UITabBarItem *tab = [self.tabBar.items objectAtIndex:i];
        tab.image = [tab.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    }
Run Code Online (Sandbox Code Playgroud)

致谢:整个互联网和堆栈溢出XD


Han*_*ney 6

在标签栏中更改文本颜色的无代码方式:

如果您只是使用iOS 10,则可以更改选项卡栏中的图像色调

在此输入图像描述

如果您还支持iOS 9及更低版本,则还必须将tintColor添加到每个标签栏项目中的用户定义器运行时属性

在此输入图像描述

如果您还想更改图标颜色,请确保您的assest文件夹中的颜色图像正确,并将Render更改为原始图像

在此输入图像描述