Jesús Bohórquez Jesús Bohórquez -5 years ago 148
Swift Question

Set custom color to UITabBarItem not working

I'm trying to change the selected text color of a TabBarItem.

If I use:

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

It works...

But if I use:

let darkColor = UIColor(red: 44, green: 62, blue: 80, alpha: 1)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : darkColor], forState: .Selected)

It does not work.

Is there any other option to change the selected text color to any color I want?


Answer Source

The UIColor initializer you are using accepts values from 0.0 to 1.0 range - you simply need to map them from 0 - 255 range you were using.

This should work :

UIColor(red: 44.0/255.0, green: 62.0/255.0, blue: 80.0/255.0, alpha: 1.0)

Related docs

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download