更改默认“未选择”UITabBarItem图像颜色

我们如何更改UITabBarItem中图标的“未选择”或未加权状态?

我尝试设置UITabBarItem.appearance()。setTitleTextAttributes(:)但它只更改文本颜色。

任何想法?

在此处输入图像描述

如果要更改iOS 7及更高版本中的默认值,则必须实际使用不同的图标(以您希望的颜色显示未选定的选项卡)并设置文本的颜色。 您可以应用此调整,而不是创建两组图标:

// set the selected colors [self.tabBar setTintColor:[UIColor whiteColor]]; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected]; UIColor * unselectedColor = [UIColor colorWithRed:184/255.0f green:224/255.0f blue:242/255.0f alpha:1.0f]; // set color of unselected text [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:unselectedColor, NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; // generate a tinted unselected image based on image passed via the storyboard for(UITabBarItem *item in self.tabBar.items) { // use the UIImage category code for the imageWithColor: method item.image = [[item.selectedImage imageWithColor:unselectedColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } 

资源。

它可以单独使用’xcode’完成。 将两个重复的“图像”集添加到“Assets.xcassets”。 以不同方式命名第二组图像,例如,将它们命名为“yourNameSelected”。 为第一个(未选中的)图标集设置“渲染为原始图像”属性:

在此处输入图像描述

将这些图像设置为未选择的位置:

将“yourNameSelected”复制图像设置为“选定图像”,然后转到“选项卡栏属性”检查器并选择所需选项卡颜色所需的“图像色调”。

如果您的目标位于ios10以下,则需要导入两种状态的彩色图像并将其渲染为原始图像。

在快速标签栏项目图标由其自己的图像呈现,所以您可以应用自己的预设

 var myImage = UIImage(named: "someImageName")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) myImageView.tintColor = UIColor.redColor() myImageView.image = myImage