Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar background color setted from storyboard differ from the color setted by code

My app will be available only for iOS 8 and I'm working with XCode 6.1.1.

If I set the color through the storyboard (setting theBar Tint attribute on Navigation Bar section)

The desired color is:

56 186 145

I used Core coding utilities to get the floating values of my color.

By code:

let backgroundColor = UIColor(red: 0.22, green: 0.729, blue: 0.569, alpha: 1.0)
self.navigationController!.navigationBar.barTintColor = backgroundColor
self.navigationController!.navigationBar.translucent = false

Color setted through storyboard is the same as the Original RGB.

  • By code: enter image description here
  • By storyboard: enter image description here

EDIT

I tried using let backgroundColor = UIColor(red: 0.255, green: 0.769, blue: 0.635, alpha: 1.0) and rgb from storyboard: 65 196 162 based on @siejkowski comment, but I get these colors:

  • By code: enter image description here

  • By storyboard: enter image description here

Why?

like image 584
Daniel Gomez Rico Avatar asked Dec 23 '14 21:12

Daniel Gomez Rico


People also ask

How do you change the background color on a storyboard?

First let us see using storyboard, Open Main. storyboard and add one view to the View Controller. On the right pane you can see the property, and from there update the background color to color you want your view to be as show below.

How do I change the background color in swift navigation?

navigationController. navigationBar. barTintColor = UIColor. newBlueColor() and of course this just changes the colour of the navigation bar of the view controller that the code is within.


1 Answers

There is two reason for the color differences here you observed.

  1. The value set in Storyboard is RGB (sRGB IEC61966-2.1) type and when you code UIColor by RGB value it will return RGB (Generic RGB) values.

So when you change color from Storyboard the values are different for RBG types. To get exact RGB value change type of RGB Sliders in Storyboard.

Click on Settings icon which is exactly right to the RGB Sliders Option. It will show a pop-up menu - select Generic RGB option.

enter image description here

Now you can observe an image that values for RGB

56 186 145

is now change to

49 175 126

So these are desire values to code.

  1. Roundup issue:

In code, you are passing giving round up values for parameter like in below line

UIColor(red: 0.22, green: 0.729, blue: 0.569, alpha: 1.0)

So it will make a small change per pixel of the color code. I suggest you divide this values by 255 and leave round up calculation for the compiler. Which will gives you desire color accuracy.

So now for new values update code will be:

let backgroundColor = UIColor(red: 49.0/255.0, green: 175.0/255.0, blue: 126.0/255.0, alpha: 1.0)
self.navigationController!.navigationBar.barTintColor = backgroundColor
self.navigationController!.navigationBar.isTranslucent = false
like image 91
Kampai Avatar answered Sep 20 '22 10:09

Kampai