How to change tab bar item text colour on focus in TVOS

允我心安 提交于 2019-12-13 00:53:20

问题


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.

This issue got in the tvos 9.1.


回答1:


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!




回答2:


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)
    }
}


来源:https://stackoverflow.com/questions/34433009/how-to-change-tab-bar-item-text-colour-on-focus-in-tvos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!