How to change tab bar item text colour on focus in

2019-08-10 16:48发布

enter image description here

I have four item in tab bar.

1> Watch. (Black Colour)

2> On Demand. (Black Colour)

3> Search. (Black Colour)

4> Settings. (My Colour)

How can i change the colour of item text in tab bar to match with its icon colour. (Right now Performance is selected in the tab bar)

enter image description here

How can I change the color of "1,2,3" text in tabbar to match with its icon color. (Right now Performance is selected in the tab bar)

I tried to set TitleTextAttributes.

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0),NSFontAttributeName: UIFont(name: "SFUIDisplay-Regular", size: 36.0)!], forState:UIControlState.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 22.0/255.0, green: 209.0/255.0, blue: 237.0/255.0, alpha: 1.0),NSFontAttributeName: UIFont(name: "SFUIDisplay-Regular", size: 36.0)!], forState:UIControlState.Focused)

But i want to like it.

enter image description here

This issue got in the tvos 9.1.

2条回答
姐就是有狂的资本
2楼-- · 2019-08-10 17:25

swift 5 solution:

override func viewDidLoad() {
    super.viewDidLoad()

    for item in (self.tabBarController?.tabBar.items)! {
        item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .focused)
        item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.lightGray], for: .normal)
    }
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-08-10 17:37

I'm not sure if I understood your question but I faced some similar issue when developing an Apple TV app.

What I wanted to do is to change the font color for the tab bar items.

This is what I ended doing. First I made a UITabBarController subclass and liked it to their controller on the storyboard. Then I added this code in the -(void)viewDidLoad

for(UITabBarItem * item in self.tabBar.items) {
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor darkGrayColor]} forState:UIControlStateNormal];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor]} forState:UIControlStateFocused];
}

And everything works pretty well.

Hope it helps!

查看更多
登录 后发表回答