Changing Tab Bar Color (Swift)

Jordan picture Jordan · Oct 27, 2014 · Viewed 14k times · Source

I am trying to change the tab bar color in a view controller in XCode using swift. I have a hex that I matched up to an RGB value and I am trying to set that in this code. (Which does not work)

let color = UIColor(red: 41, green: 40, blue: 39, alpha: 1.0)
UITabBar.appearance().barTintColor = color

However this code does:

UITabBar.appearance().barTintColor = UIColor.whiteColor()

Can anyone explain why this doesn't work, and what I can do to fix it?

Answer

Ron Fessler picture Ron Fessler · Oct 27, 2014

To use RGB values, just divide them by 255.0. This will produce a float value between 0 and 1.

let color = UIColor(red: 41.0/255.0, green: 40.0/255.0, blue: 39.0/255.0, alpha: 1.0)