2014-09-13 119 views
6

非常简单,我希望能够更改我的选项卡中未选项目的颜色。更改UITabBarItem未选中的颜色色调 - Swift

请参阅下面的“最多查看”对象大麦可读的默认颜色。

这里是代码,我试图执行:

UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIColor.greenColor(), forKey: NSFontAttributeName), forState: UIControlState.Normal) 

enter image description here

但是,使用此代码不能正常工作。有谁知道如何快速实现这种效果?


回答

2

UITabBarItem从类文档:

默认情况下,未被选择的实际和选定的图像是 从源图像中的α值自动创建。到 防止系统着色,提供图像与 UIImageRenderingModeAlwaysOriginal。

线索不是你是否使用UIImageRenderingModeAlwaysOriginal,重要的是什么时候使用它。

要防止未选定项目的灰色,您只需要防止未选定图像的系统着色。这里是如何做到这一点:

var firstViewController:UIViewController = UIViewController() 
// The following statement is what you need 
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME")) 
firstViewController.tabBarItem = customTabBarItem 

正如你所看到的,我问的iOS应用图像的原始颜色(白色,黄色,红色,等等)仅适用于未选中状态,并留下该图像作为它用于SELECTED状态。

此外,您可能需要为选项卡栏添加色调颜色,以便为SELECTED状态(而不是默认的iOS蓝色)应用不同的颜色。根据上面的屏幕截图,您正在为选定状态应用白色:

self.tabBar.tintColor = UIColor.whiteColor() 
0

似乎只是一个语法错误;尝试这样的:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.greenColor()], forState: .Normal) 

或(包括图像,如果上面没有):

UITabBarItem.appearance().setTintColor(UIColor.greenColor());