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

时间:2015-06-05 10:40:25

标签: ios uitabbar uitabbaritem

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

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

有什么想法吗?

enter image description here

3 个答案:

答案 0 :(得分:4)

如果要更改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];
}

Source.

答案 1 :(得分:1)

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

enter image description here

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

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

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

答案 2 :(得分:0)

在swift标签栏中,项目图标由其自己的图像显示,因此您可以应用自己的预设

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