user3734410 user3734410 - 3 months ago 19
Swift Question

UINavigationItem RGB Swift

I'm currently having difficulty setting the background color of a UINavigationItem. The following line of code is the only one that has worked so far:

self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.55, green: 0.55, blue: 0.55, alpha: 1)


But the problem with this line is that the color of the bar is not the same as the rgb value should be. I have tried to change the translucency with the following line:

self.navigationController?.navigationBar.translucent = false


But this does not work either. I expected to get a dark gray color equivalent to the hex value #333333. But instead I received a lighter gray. Any help to remedy the problem is greatly appreciated!

Answer

To take full control of a navigation bar's color, do this:

  • Set its translucent to false as you are doing

  • Make (in code) a UIImage that is simply a rectangular swatch of the desired color, and set that as the navigation bar's backgroundImage (for whatever the relevant metrics is, usually default.

Now the navigation bar will be exactly the color you asked for.

Comments