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

问题描述 投票:27回答:11

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

uitabbarcontroller uitabbar tabbar uitabbaritem ios7
11个回答
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 *

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

0
投票

@Usharao上面的答案对我有用;

我的问题是在启动时我的所有TabBarItems似乎处于选定状态,都具有相同的“蓝色”着色。通过逐个选择所有标签,彩色状态将被纠正。

我在AppDelegate类中使用了以下代码:(兼容> = IOS9)

[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UITabBar class]]] 
                                        setTintColor:[UIColor lightGrayColor]];

0
投票

现在从iOS10可以使用

@property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor

更改TabBarItem图像和未选定状态的文本的默认颜色。

因此,这对属性tintColorunselectedItemTintColor让我们完全控制项目的颜色。


43
投票

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

UITabBarItem *item = [self.tabBar.items objectAtIndex:1];

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

item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

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

self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected.png"];

11
投票

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

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

self.tabBarController.tabBar.translucent = false;

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

self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
self.tabBarController.tabBar.translucent = false;
self.tabBarController.tabBar.tintColor = [UIColor blueColor];

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

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

在UITabBarController子类-awakeFromNib中:

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

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


6
投票

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

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

enter image description here

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

enter image description here

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

enter image description here


3
投票

这应该适用于iOS 8

对于未选中的tabbar项:

[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor: [UIColor whiteColor]];

对于选定的标签栏项目:

[[UITabBar appearance] setTintColor:[UIColor orangeColor]];

2
投票
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           nil] 

0
投票

使用self.tabBarController.tabBar.barStyle = UIBarStyleBlack;使标签栏变黑


0
投票

你试一下

for (UITabBarItem *item in self.tabBarController.tabBar.items) {
        item.image = [[UIImage imageNamed:@"youimage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           nil] forState:UIControlStateNormal];

        item.selectedImage = [UIImage imageNamed:@"youimage.png"];
    }
© www.soinside.com 2019 - 2024. All rights reserved.