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

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:
By storyboard:
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?

There is two reason for the color differences here you observed.
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.
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.
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

You see the difference in colors because they are set using the same RGB values, but in different color-spaces.
UIColor, when set from code, interprets the values as from sRGB space, as provided here: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGColorSpace/index.html
However, it seems that the Storyboard color picker is using native values RGB by default. You need to convert between the two. You can do it in Storyboard color picker by clicking the settings icon next to RGB Sliders list picker in Colors tab. Native RGB values are set under Color LCD setting and sRGB values are under sRGB, of course.
When you do it, is seems that 56 186 145 in native RGB space is 73 186 141 in sRGB space. So your code needs to be:
let backgroundColor = UIColor(red: 0.286, green:0.729, blue:0.553, alpha:1.0)

When I try to change the color space to GenericRGB from the storyboard/Interface builder (on XCode 6.3.2), XCode does not save the change. So if I reopen the color from Interface builder the color space is always sRGBIEC61966-2.1.
So I came up a different solution that may be useful for someone else:
with the Mac OS default app "ColorSync Utility" I used the Calculator and translate the value of my color from GenericRGB to a sRGBIEC61966-2.1. Then I set the translated value into the Interface builder and I obtained the same result as was obtaining it programmatically!

Related

How to use Color with RGB or hex value in SwiftUI using Swift 5? [duplicate]

This question already has answers here:
How can I create a UIColor from a hex string?
(49 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
I am currently trying to change the background color of a Label in SwiftUI. As far as I know, SwiftUI takes a View as the parameter for the background Modifier.
However, I encountered a problem I am not able to fix:
I would like to change the background color of my Label by using a hex code or an RGB value. Because of some reason, when doing so the background color always changes to white.
This works perfectly fine:
Label(
title: { Text("someTitle") },
icon: { Image(systemName: "someName") }
)
.background(Color.purple) // Using the provided standard Colors works perfectly fine.
However, I would like to create a background color using a hex or RGB value:
.background(Color(Color.RGBColorSpace.sRGB, red: 92, green: 225, blue: 230, opacity: 1))
// or
.background(Color(UIColor(red: 220, green: 24, blue: 311, alpha: 1)))
This does not work and the background color of the Label always changes back to white. How to achieve this goal?
The UIColor init method UIColor(red:green:blue:alpha) takes float values from 0 to 1 for each value.
Any value ≥ 1.0 is going to "max out" that color. So an expression like UIColor(red: 220, green: 24, blue: 311, alpha: 1) will set all 3 color channels and alpha to 1.0, resulting in white. So would UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
You could create an extension to UIColor that adds convenience initializers that would take values of different types. First, and initializer that would take values from 0-255 for each. Then you could create another intializer UIColor.init(hexString:String).
The one that took values from 0-255 would be a lot easier to write. The one that took hex strings would involve implementing a hex parser. I bet somebody has already written it though.
Take a look at this code, for example:
https://gist.github.com/anonymous/fd07ecf47591c9f9ed1a

How to set a custom background color to UIButton?

I was wondering about different types of code for changing the background colour of the button. In my project, I am using:
button4.backgroundColor = UIColor.green
But it only gives me a restricted amount of colour options since it is UIColor. I was wondering if I can write a different piece of code to have more flexibility in the color of the button.
Thanks for any help given.
But it only gives me a restricted amount of colour options since it is
UIColor.
That would be correct when working with the predefined static colors. However, no doubt that you can get another color which is none of the predefined ones.
For instance, consider you have a custom rgb color that you want to use in your app (the button background color), as: red: 100 green: 44 blue: 63, You could get it by using:
init(red:green:blue:alpha:)
Initializes and returns a color object using the specified opacity and
RGB component values.
as follows:
let myCustomRGBColor = UIColor(red: 100.0/255.0, green: 44.0/255.0, blue: 63.0/255.0, alpha: 1.0)
Note that the values are divided on 255.0 because rgb values for the UIColor are between 0 and 1.
Also, note that UIColor has many other initializers for creating a custom color object.

UIButton title color RGB values programmatically different than what was set in Interface Builder

I added a UIButton to my view. It's a regular button of system type, default config and plain title, all default values. I set the text color property in IB to the RGB values shown below: r68, g66, b62
I made a custom color programmatically with the same RGB values:
extension UIColor {
class var customColor: UIColor {
return UIColor(red: 68/255, green: 66/255, blue: 62/255, alpha: 1)
}
}
Why are the button's RGB values different when I check them in my view controller than what I set them to be in IB (identical to my custom color RGB values)?
// UIExtendedSRGBColorSpace 0.33669 0.327454 0.310221 1
myButton.titleColor(for: .normal)!.description)
// UIExtendedSRGBColorSpace 0.266667 0.258824 0.243137 1
UIColor.customColor.description
Shouldn't they be the same? If not, how do I get them to be the same?

Using RGB in AppDelegate yet colors do not match fully(Hex)

I am working on finalizing my app before I submit to the store.One issue that I am coming across is that the RGB that I am using is not holding true to the color that I am trying to show.
Here is a screenshot:
I want the below the search bar to be the color throughout the app, however Xcode is pushing the following HEX code: #323A45 to a lighter grey then it should be.
Here is the actual color this should be: http://www.colorhexa.com/323a45
Finally, here is the code for my navigation bar:
The below code is in the appDelegate file
// Navigation Bat
UINavigationBar.appearance().barTintColor = UIColor(rgba: "#323A45")
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
UINavigationBar.appearance().translucent = false
Hopefully I am just missing something. I have also used the color codes divided by 255. Ex. 45/255 55/255 etc. with the same result as you are seeing.
Can you help me on what I am doing wrong?
You could try something like this
UINavigationBar.appearance().tintColor = UIColor(red: 39.0 / 255.0, green: 46.0 / 255.0, blue: 56.0/ 255.0, alpha: 1.0)
I have added the colour you provided, please try it and confirm.
EDIT
Please do this too-

Swift UIView background color opacity

I have a UIView with a UILabel in it. I want the UIView to have white background color, but with an opacity of 50%. The problem whith setting view.alpha = 0.5 is that the label will have an opacity of 50% as well, so I figured out that it maybe would be possible to have a UIView with white background color and opacity (white_view), and then have another UIView with the label (label_view). Then add the "white_view" to "label_view" by doing this: label_view.addSubview(white_view). This apparently doesn't work. I'd like to do like: label_view.backgroundView(white_view) but you can't set a background view on a UIView like you can do in a UICollectionView for instance.
Does anyone have any clue of how to solve this?
EDIT
Because several answers are approx the same I'll type it here.
Now I've tried even these:
label_view1.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
label_view1.addSubview(firstPlacelbl)
endGameView.addSubview(label_view1)
and
label_view1.backgroundColor = UIColor(white: 1, alpha: 0.5)
label_view1.addSubview(firstPlacelbl)
endGameView.addSubview(label_view1)
And still the label is also affected by the alpha, and it gets an opacity of 50%. I don't get it what I do wrong because I only set the colors alpha to 0.5 and not the labels. Any ideas?
You can set background color of view to the UIColor with alpha, and not affect view.alpha:
view.backgroundColor = UIColor(white: 1, alpha: 0.5)
or
view.backgroundColor = UIColor.red.withAlphaComponent(0.5)
Setting alpha property of a view affects its subviews. If you want just transparent background set view's backgroundColor proprty to a color that has alpha component smaller than 1.
view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
For Swift 4.x and above
yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
You can also set it from InterfaceBuilder by changing color's opacity:
The problem you have found is that view is different from your UIView. 'view' refers to the entire view. For example your home screen is a view.
You need to clearly separate the entire 'view' your 'UIView' and your 'UILabel'
You can accomplish this by going to your storyboard, clicking on the item, Identity Inspector, and changing the Restoration ID.
Now to access each item in your code using the restoration ID
The question is old, but it seems that there are people who have the same concerns.
What do you think of the opinion that 'the alpha property of UIColor and the opacity property of Interface Builder are applied differently in code'?
The two views created in Interface Builder were initially different colors, but had to be the same color when the conditions changed.
So, I had to set the background color of one view in code, and set a different value to make the background color of both views the same.
As an actual example, the background color of Interface Builder was 0x121212 and the Opacity value was 80%(in Amani Elsaed's image :: Red: 18, Green: 18, Blue: 18, Hex Color #: [121212], Opacity: 80),
In the code, I set the other view a background color of 0x121212 with an alpha value of 0.8.
self.myFuncView.backgroundColor = UIColor(red: 18, green: 18, blue: 18, alpha: 0.8)
extension is
extension UIColor {
convenience init(red: Int, green: Int, blue: Int, alpha: CGFloat = 1.0) {
self.init(red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: alpha)
}
}
However, the actual view was
'View with background color specified in Interface Builder': R 0.09 G 0.09 B 0.09 alpha 0.8.
'View with background color by code': R 0.07 G 0.07 B 0.07 alpha 0.8
Calculating it,
0x12 = 18(decimal)
18/255 = 0.07058...
255 * 0.09 = 22.95
23(decimal) = 0x17
So, I was able to match the colors similarly by setting the UIColor values ​​to 17, 17, 17 and alpha 0.8.
self.myFuncView.backgroundColor = UIColor(red: 17, green: 17, blue: 17, alpha: 0.8)
Or can anyone tell me what I'm missing?
It's Simple in Swift . just put this color in your background view color and it will work .
let dimAlphaRedColor = UIColor.redColor().colorWithAlphaComponent(0.7)
yourView.backGroundColor = dimAlphaRedColor

Resources