在Swift中只为一个tabBarItem设置tintColor

时间:2017-09-11 09:47:46

标签: ios swift swift3 uitabbarcontroller

我正在尝试将tintColor设置为我标签栏的一个tabBarItem。

目前,我有:

self.tabBar.tintColor = darkGrey
self.tabBar.unselectedItemTintColor = grey

我的标签栏中有5个tabBarItems(没有文字),我想在选择项目时将第一个设置为蓝色。

我该怎么做?

6 个答案:

答案 0 :(得分:3)

您必须为每个tabBarItem单独设置它。 您可以通过将此代码放在viewDidLoad()上来更改特定选项卡的选项卡栏的颜色。

//setting your tabBarItem to blue when selected
self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue()], for:.selected)

答案 1 :(得分:1)

选择UITabbarItem并添加运行时属性'selectedImage',选择Type作为'Image'并将图像名称作为其值。

enter image description here

或按代码。

UITabBar *tabBar = self.tabBar;

UITabBarItem *targetTabBarItem = [[tabbar items] objectAtIndex:0]; // 
whichever tab-item
UIImage *selectedIcon = [UIImage imageNamed:@"name-of-selected-
image.png"];
[targetTabBarItem setSelectedImage:selectedIcon];

答案 2 :(得分:1)

您可以在故事板中进行编辑 图片色调所选图标的颜色
背景是标签栏的颜色
Tint 是图标的颜色


请检查下面的图像

https://i.stack.imgur.com/7Jnwy.png

答案 3 :(得分:0)

如果您只更改特定标签的色调,那么这将有所帮助。

var tab1 : UITabBarItem = self.tabBar.items![0] as UITabBarItem
tab1.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for: .normal)

//For Image
tab1.image = UIImage(named: "imageName")!.withRenderingMode(.alwaysOriginal)

答案 4 :(得分:0)

这是另一种方式:

self.tabBar.tintColor = UIColor.red
let tabSelected = self.tabBar.selectedItem!
tabSelected.selectedImage = #imageLiteral(resourceName: "second")

答案 5 :(得分:0)

我的原始代码

tabaritem.image = UIImage(named:imageName)?. withRenderingMode(.alwaysOriginal)

我将 alwaysOriginal改为自动,它对我有用

tabaritem.image = UIImage(named:imageName)?. withRenderingMode(.automatic)