【发布时间】:2013-09-15 02:24:17
【问题描述】:
如何在 iOS 7 中更改 UITabBar 和 UITabBarItems 的文本和图标颜色?对于未选择的标签栏项目,默认的灰色文本看起来很暗,难以阅读。
【问题讨论】:
标签: uitabbarcontroller uitabbar tabbar uitabbaritem ios7
如何在 iOS 7 中更改 UITabBar 和 UITabBarItems 的文本和图标颜色?对于未选择的标签栏项目,默认的灰色文本看起来很暗,难以阅读。
【问题讨论】:
标签: uitabbarcontroller uitabbar tabbar uitabbaritem ios7
为此,您需要做两件事:
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];
【讨论】:
使用self.tabBarController.tabBar.barStyle = UIBarStyleBlack; 使标签栏变黑
【讨论】:
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];
【讨论】:
这对我有用,可以为标签栏中的非活动项目着色
UITabBarItem *item = [self.tabBar.items objectAtIndex:1];
//这里你需要使用你想要的颜色的图标,因为它会被渲染成原来的样子
item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 此图标用于选定的选项卡,它将按照
中的定义进行着色self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected.png"];
【讨论】:
imageWithRenderingMode是改变标签图片颜色的关键
你试试
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"];
}
【讨论】:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil]
【讨论】:
这应该也适用于 iOS 8
对于未选中的标签栏项目:
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor: [UIColor whiteColor]];
对于选定的标签栏项目:
[[UITabBar appearance] setTintColor:[UIColor orangeColor]];
【讨论】:
在 iOS 8 中测试了永久文本颜色(选中/未选中)和图像颜色(选中/未选中)没有为每个选项卡创建两个具有不同颜色的图像:
文字颜色:
[[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
【讨论】:
@Usharao 上面的回答对我有用;
我的问题是在启动时我的所有 TabBarItems 似乎处于选定状态,都具有相同的“蓝色”着色。 通过一一选择所有选项卡,颜色状态将得到纠正。
我在 AppDelegate 类中使用了以下代码:(兼容 >= IOS9)
[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UITabBar class]]]
setTintColor:[UIColor lightGrayColor]];
【讨论】:
现在iOS10可以使用了
@property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor
更改TabBarItem 未选中状态下图像和文本的默认颜色。
因此,tintColor 和 unselectedItemTintColor 这对属性让我们可以完全控制项目的颜色。
【讨论】: