更改UITabBarItem未选择的颜色色调 - 斯威夫特

时间:2014-09-13 17:00:55

标签: ios swift uitabbar uicolor uitabbaritem

很简单,我希望能够更改标签栏中未选中项目的颜色。

见下面"观看次数最多"对象大麦可以默认颜色读取。

以下是我尝试实施的代码:

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

enter image description here

但是,使用此代码并不起作用。有谁知道如何特别在swift中实现这种效果?


3 个答案:

答案 0 :(得分:3)

来自UITabBarItem课程文档:

  

默认情况下,实际未选择和选择的图像是   根据源图像中的Alpha值自动创建。至   防止系统着色,提供图像   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仅为UNSELECTED状态应用图像的原始颜色(白色,黄色,红色等),并将图像保留为SELECTED状态。

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

self.tabBar.tintColor = UIColor.whiteColor()

答案 1 :(得分:1)

快捷键4:

您可以使用unselectedItemTintColor中的UITabBar。请注意,它会更改所有未选择的图标的颜色。

用法:

myTabBar.unselectedItemTintColor = .black

答案 2 :(得分:0)

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

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

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

UITabBarItem.appearance().setTintColor(UIColor.greenColor());
相关问题