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.
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:
By storyboard:
Why?
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.
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.
There is two reason for the color differences here you observed.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With