在 swift 中取消选择时,setTitleTextAttributes 不适用于 UITabBarItem

setTitleTextAttributes doesn't work for UITabBarItem when it is unselected in swift

我正在尝试自定义 UITabBarController。我将它嵌入到我的 UIViewController 中,我还为此 UITabBarController 创建了一个 class。

override func viewDidLoad() {
    super.viewDidLoad()

    //custom tab bar
    self.tabBar.barTintColor = UIColor(red: 0.0/255.0, green: 102.0/255.0, blue: 153.0/255.0, alpha: 1)
    self.tabBar.tintColor = UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)
    self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
    self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Disabled)

    for item in self.tabBar.items as [UITabBarItem]
    {
        item.image = item.selectedImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    }


    // Do any additional setup after loading the view.
}

setTitleTextAttributes 对标签栏项目没有任何影响。有人可以帮我找出错误所在吗?

标签栏项目属于各个子视图控制器,因此您需要更改这些控制器中的属性,而不是标签栏控制器。

这里是在这种情况下仍然可以放在 UITabBarController 中的代码:

override func viewDidLoad() {
super.viewDidLoad()

//custom tab bar
self.tabBar.barTintColor = UIColor(red: 0.0/255.0, green: 102.0/255.0, blue: 153.0/255.0, alpha: 1)
self.tabBar.tintColor = UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)

for item in self.tabBar.items as [UITabBarItem]
{
    item.image = item.selectedImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
    item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Disabled)
    item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)], forState:UIControlState.Selected)
}
}