UITabBarItem颜色只能更改一次?

时间:2015-09-23 17:37:13

标签: ios swift

我想根据发生的事件动态更改底部标签文本颜色。标签文本颜色允许您更改一次,但第二次不能工作?

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.blackColor()], forState: UIControlState.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.whiteColor()], forState: UIControlState.Selected)

我需要多次更改标签栏文字颜色,但它不允许我。我只能改一次吗?调用上面的代码时,颜色第二次保持不变

2 个答案:

答案 0 :(得分:0)

我认为你应该使用" if循环",代码看起来像这样:

if condition == "Whatever"{
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.blackColor()], forState: UIControlState.Normal)
}else{
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.whiteColor()], forState: UIControlState.Selected)
}

请注意,条件应该是您所说的任何内容:"发生了什么事件"以及""上的文字应该用你事件发生的任何事情来代替。

如果我确实给了您帮助,请给我积极的评价。亲切的问候!

答案 1 :(得分:0)

我想通了

self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : color], forState: .Selected)
相关问题