Swift - Custom color not applying to barTintColor [duplicate] - swift

This question already has answers here:
UIColor not working with RGBA values
(6 answers)
Closed 6 years ago.
In a view controller embedded in a navigation controller I am attempting to change the barTintColor to a custom color. What I have experienced is if I use a default color such as the one below, the color is actually applied:
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
However when I try to create an instance of my own custom color such as this code would show, the color is not applied:
let customRedColor = UIColor( red: 255, green: 0, blue: 13, alpha: 1 )
self.navigationController?.navigationBar.barTintColor = customRedColor
I am very curious as to why the custom color is not being applied to the navigation bar so that leads me to ask:
What approach should be taken in order to correctly apply a custom color to the navigation bars barTintColor property.

You need to give color by dividing them by 255, the syntax is like this.
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
Each of the ones that take a CGFloat take a value between 0.0 and 1.0, referring to either the complete absence of, or the maximum amount of that color component respectively. So, this means that even if you have pure RGB values in decimal or hexadecimal format, you will have to divide them by decimal 255 to get the amount to input here.
let customRedColor = UIColor(red: 255/255.0 , green: 0, blue: 13/255.0, alpha: 1.0)
//or Direct
let customRedColor = UIColor(red: 1.0 , green: 0, blue: 0.05, alpha: 1.0)
self.navigationController?.navigationBar.barTintColor = customRedColor

Related

Set custom background color on a stack in Swift (SwiftUI)

My code currently consists of:
VStack{
Text("Something")
}
.background(Color.red)
I want to use a custom color for the background instead of Color.red. However, looking at the documentation, it seems like the background function takes in a background. Therefore, I can't declare a variable:
let exampleColor : Color = Color(red: 141, green: 223, blue: 144)
and use it within the background function. How do I tackle this problem?
VStack{
Text("Something")
}
.background(exampleColor)
Doesn't work.
You have to set values between 0 to 1 for RGB Colors. Use the following
let exampleColor : Color = Color(red: 141/255, green: 223/255, blue: 144/255)
or value between 0 to 1 like
let exampleColor : Color = Color(red: 0.5, green: 0.8, blue: 0.5)

Creating CGColor from RGB Value

I use the following code to set the background of a viewcontroller
view.wantsLayer = true
let myColor = NSColor(calibratedRed: 50, green: 50, blue: 50, alpha: 1.0)
view.layer?.backgroundColor = myColor.cgColor
But on debugging myColor i get the following color instead of the intended color
Here is the documentation. Anything above 1 is considered as 1. You need to divide each value by 255.
view.wantsLayer = true
let myColor = NSColor(calibratedRed: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
view.layer?.backgroundColor = myColor.cgColor
Also, check this out -> Link
Range of color components (RGBA) vary between [0,1]
init(red:green:blue:alpha:)
Creates a color object with the specified red, green, blue, and alpha
channel values. This method accepts extended color component values.
If the red, green, blue, or alpha values are outside of the 0-1.0
range,
So you can get cgColor
view.layer?.backgroundColor = NSColor(calibratedRed: 50.0/255.0, green: 50.0/255.0, blue: 50.0/255.0, alpha: 1.0).cgColor

Setting tintColor for Apple Watch complication

I am trying to set the header text color for a Modular Large complication.
I have already customized the watch face to use Multicolor.
However, when I build and run this code, the header text color is still white (which is the default).
Why isn't the color updating?
private func templateForClassModularLarge(className: Schedule) -> CLKComplicationTemplateModularLargeStandardBody {
let template = CLKComplicationTemplateModularLargeStandardBody()
let headerTextProvider = CLKSimpleTextProvider(text: "My Schedule", shortText: "Schedule")
headerTextProvider.tintColor = UIColor(red: 101, green: 153, blue: 255, alpha: 1)
template.headerTextProvider = headerTextProvider
template.body1TextProvider = CLKTimeIntervalTextProvider(startDate: className.start, endDate: className.end)
template.body2TextProvider = CLKSimpleTextProvider(text: className.description, shortText: className.shortDescription)
return template
}
UIColor parameter types are CGFloat, specified as a value from 0.0 to 1.0.
Because your RGB parameters are greater than 1, the color ends up being white, which would be:
UIColor(red: 1, green: 1, blue: 1, alpha: 1)
To fix this issue, simply change your tintColor to
headerTextProvider.tintColor = UIColor(red: 101/255, green: 153/255, blue: 255/255, alpha: 1)

Swift - why NSColor becomes lighter when rendered

I've created NSWindow and made it's background colour absolutely blue (#0000FF). But when the window is rendered, the colour is "lighter" than it should be (#0F3FFB).
class LilWindow: NSViewController {
override func viewDidLoad() {
self.view.window?.backgroundColor =
NSColor.init(red: 0, green: 0, blue: 1, alpha: 1)
}
Does anyone know why it is happening and how to fix this? (screenshot attached)
Okay, so after a couple of hours fiddling with code and #KenThomases help, I figured out that if you want your RGB colours to looks correctly on NSImages and NSWindows, you must convert it into NSDeviceRGBColorSpace colorspace. To do this I've written a simple function:
func toScreenColor(color:NSColor) -> NSColor {
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
color
.colorUsingColorSpaceName(NSCalibratedRGBColorSpace)!
.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return NSColor(deviceRed: red, green: green, blue: blue, alpha: alpha)
}

Difference between Color and UIColor in Swift?

Whats the difference between using
let magenta = Color(red: 1.0, green: 0.0, blue: 1.0)
and
let magenta = UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha:1.0)
?
I believe your question has been answered in the comments to your question, but to provide a complete answer:
Class Color does not exist as a pre-defined type in Swift's UIKit. In the example you linked to, that is just a custom struct that Apple created to demonstrate how to initialize structs; you cannot actually use that struct in your own code without creating it again yourself (which normally you wouldn't want to do).
Instead, you'll want to create colors using UIColor, which does come pre-defined in Swift's UIKit. You can find the documentation here. The second example you provided is, in fact, the correct usage of UIColor:
let magenta = UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha:1.0)
Note that, as of the release of SwiftUI, there actually does now exist a class called Color, but this would only be used if you are using the SwiftUI framework, which is obviously not what you were asking about back in 2014.