Unable to set custom colours to Navigation bar items iOS Swift - ios

I am using the below code in my AppDelegate to set the colours for the Navigation Bar items. It works when I use the default colours (Step 1) and doesn't work when I use custom colours (Step 2).
Could someone please suggest how I could fix this ?
Step1: Works
// Set navigation bar, background color
UINavigationBar.appearance().barTintColor = UIColor.red
Step 2: Doesn't Work
// Set navigation bar, background color
UINavigationBar.appearance().barTintColor = UIColor(red: 41, green: 150, blue: 204, alpha: 1)
Here is my extension for my UIColor, implemented in one of my styles.swift file
extension UIColor {
convenience init(_ r: Double,_ g: Double,_ b: Double,_ a: Double) {
self.init(red: CGFloat(r/255), green: CGFloat(g/255), blue: CGFloat(b/255), alpha: CGFloat(a))
}
}

Try this
extension UIColor {
static func rgb( red : CGFloat, green : CGFloat, blue : CGFloat) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
}
Step 2 :
UINavigationBar.appearance().barTintColor = UIColor.rgb(41,150,204)

You are using the default initialiser and not the one implemented in the extension :
Remove the params in your code like so :
UINavigationBar.appearance().barTintColor = UIColor(41, 150, 204,1)

UIColor's init takes CGFloat parameters, and CGFloat is a typedef for Double. Try to change your extension to accept Int as parameters instead of Double

You are not actually correctly using your extension.
To use it, call like this,
UINavigationBar.appearance().barTintColor = UIColor(41,150,204,1)
When you use underscore _ character for method names in the method definition, you have to omit method names when calling it.

Your extension is wrong. You have to divide by a float. As of now you are dividing by an Int so you are probably just getting 0 or 1.
It should be "r/255.0" etc...
EDIT: And as others have pointed out, you are not using your extension.
It should be
UIColor(41, 150, 204, 1)

On your UIColor Extension, do you have a need to define a convenience init? or can't you just use:
public func rgba(_ r:Int, _ g:Int, _ b:Int, _ a:CGFloat) -> UIColor {
return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: a)
}

Related

Supported default light/dark mode colors for Color vs UIColor

I'm trying to use Apple’s built in colors (UIColor.label, UIColor.secondaryLabel, etc), but I can't seem to find their Color equivalent. Since I can't use them as a Color, I can't use them in my SwiftUI code. Is there any way to use these colors in SwiftUI?
I was able to modify some code I found here to make solution. Not the best solution in the world, but it appears to work.
extension Color {
static var label = Color.from(uicolor: .label)
static func from(uicolor: UIColor) -> Color {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
uicolor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return Color(red: Double(red), green: Double(green), blue: Double(blue)).opacity(Double(alpha))
}
}
Usage:
Text("Test").color(.label)

Using Extension for Converting Hex to UIColor [duplicate]

This question already has answers here:
How to use hex color values
(39 answers)
Closed 8 years ago.
I am trying to change the background color of TopScoreContainer to a lighter shade of green. I do not want to use greenColor() . Here is the line of code:
self.TopScoreContainer.backgroundColor = UIColor.greenColor()
Is it possible to substitute in a hexadecimal number or RGB value instead of greenColor() ? Thanks.
let myCustomColorHSBa = UIColor(hue: 120/360, saturation: 0.25 , brightness: 1.0 , alpha: 1)
let myCustomColorRGBa = UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)
using it as an extension read-only computed var:
Read-Only Computed Properties
A computed property with a getter but no setter is known as a
read-only computed property. A read-only computed property always
returns a value, and can be accessed through dot syntax, but cannot be
set to a different value.
NOTE
You must declare computed properties—including read-only computed
properties—as variable properties with the var keyword, because their
value is not fixed. The let keyword is only used for constant
properties, to indicate that their values cannot be changed once they
are set as part of instance initialization.
You can simplify the declaration of a read-only computed property by
removing the get keyword and its braces:
extension UIColor {
var lightGreen: UIColor {
return UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)
}
}
let lightGreen = UIColor().lightGreen
or you can also create your own htmlColor input as follow:
update: Xcode 7.2 • Swift 2.1.1
extension String {
subscript(range: Range<Int>) -> String {
return range.startIndex < 0 || range.endIndex > characters.count ? "Out of Range" : substringWithRange(Range(start: startIndex.advancedBy(range.startIndex),end: startIndex.advancedBy(range.endIndex)))
}
var hexaCGFloat: CGFloat {
return CGFloat(strtoul(self, nil, 16))
}
}
extension UIColor {
convenience init(htmlColor: String, alpha: Double) {
self.init(red: htmlColor[1...2].hexaCGFloat / 255.0, green: htmlColor[3...4].hexaCGFloat / 255.0, blue: htmlColor[5...6].hexaCGFloat / 255.0, alpha: CGFloat(alpha) )
}
convenience init(r: Int, g:Int , b:Int , a: Int) {
self.init(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: CGFloat(a)/255)
}
}
let myColor = UIColor(r: 255 , g: 0, b: 0, a: 255)
let myHtmlWebColor = UIColor(htmlColor: "#bfffbf", alpha: 1.0)

IOS Example of initWithCGColor

I give in, this is taking too long and I can't find the answer listed anywhere.
In the Framework Reference for UIColor there is a function initWithCGColor. How do you use this? Can someone please help with example? I am looking to initialize a color to a specific value!
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/index.html#//apple_ref/occ/instm/UIColor/initWithCGColor:
CGColorRef is a class used for drawing using Core graphics. If you want to initialise an UIColorobject with a specific RGB value, use
- initWithRed:green:blue:alpha:
Swift
let color = UIColor(red: 59/255.0, green: 136/255.0, blue: 195/255.0, alpha: 1)
Objective-C
UIColor * color = [UIColor colorWithRed:59/255.0f green:136/255.0f blue:195/255.0f alpha:1];
For helping purposes, I add some useful extension/macro for UIColor in order to initialise objects without having to consider the division by 255.
I am not the creator of this code but a grateful user who wants to share some time-saving code :
Swift
public extension UIColor{
class func initRGBA(r r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor
{
return UIColor(red:r/255, green: g/255, blue: b/255, alpha: a)
}
class func initRGB(r r:CGFloat, g:CGFloat, b:CGFloat) -> UIColor
{
return UIColor.initRGBA(r:r, g:g, b:b, a:1)
}
class func initRGBGRAY(gray:CGFloat) -> UIColor
{
return UIColor.initRGBA(r:gray, g:gray, b:gray, a:1)
}
convenience init(rgb: UInt) {
self.init(
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgb & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
/// Let's initialize a color
let color = UIColor.initRGB(r: 24, g: 80, b: 145)
Objective-C
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define RGBGRAYCOLOR(g) [UIColor colorWithRed:(g)/255.0f green:(g)/255.0f blue:(g)/255.0f alpha:1]
/// Let's initialize a color
UIColor * color = RGBCOLOR(87, 99, 132);

How do I pick the color in hexadecimal form or RGB form instead of using the colors given to me in Swift [duplicate]

This question already has answers here:
How to use hex color values
(39 answers)
Closed 8 years ago.
I am trying to change the background color of TopScoreContainer to a lighter shade of green. I do not want to use greenColor() . Here is the line of code:
self.TopScoreContainer.backgroundColor = UIColor.greenColor()
Is it possible to substitute in a hexadecimal number or RGB value instead of greenColor() ? Thanks.
let myCustomColorHSBa = UIColor(hue: 120/360, saturation: 0.25 , brightness: 1.0 , alpha: 1)
let myCustomColorRGBa = UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)
using it as an extension read-only computed var:
Read-Only Computed Properties
A computed property with a getter but no setter is known as a
read-only computed property. A read-only computed property always
returns a value, and can be accessed through dot syntax, but cannot be
set to a different value.
NOTE
You must declare computed properties—including read-only computed
properties—as variable properties with the var keyword, because their
value is not fixed. The let keyword is only used for constant
properties, to indicate that their values cannot be changed once they
are set as part of instance initialization.
You can simplify the declaration of a read-only computed property by
removing the get keyword and its braces:
extension UIColor {
var lightGreen: UIColor {
return UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)
}
}
let lightGreen = UIColor().lightGreen
or you can also create your own htmlColor input as follow:
update: Xcode 7.2 • Swift 2.1.1
extension String {
subscript(range: Range<Int>) -> String {
return range.startIndex < 0 || range.endIndex > characters.count ? "Out of Range" : substringWithRange(Range(start: startIndex.advancedBy(range.startIndex),end: startIndex.advancedBy(range.endIndex)))
}
var hexaCGFloat: CGFloat {
return CGFloat(strtoul(self, nil, 16))
}
}
extension UIColor {
convenience init(htmlColor: String, alpha: Double) {
self.init(red: htmlColor[1...2].hexaCGFloat / 255.0, green: htmlColor[3...4].hexaCGFloat / 255.0, blue: htmlColor[5...6].hexaCGFloat / 255.0, alpha: CGFloat(alpha) )
}
convenience init(r: Int, g:Int , b:Int , a: Int) {
self.init(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: CGFloat(a)/255)
}
}
let myColor = UIColor(r: 255 , g: 0, b: 0, a: 255)
let myHtmlWebColor = UIColor(htmlColor: "#bfffbf", alpha: 1.0)

Where do you put a global method in swift?

I have a function that is common to all my controllers:
func myColor() -> UIColor {
return UIColor(red: 9.0/255.0, green: 134.0/255.0, blue: 255.0/255.0, alpha: 1)
}
Where can I put this function, so I can access it from any controller?
By default all default access scope (internal) functions are available everywhere in the application. If you have this function defined in different module, you need to use public modifier.
To make your code clearer it is best to create extension for UIColor.
extension UIColor {
class func myColor() -> UIColor {
return UIColor(red: 9.0/255.0, green: 134.0/255.0, blue: 255.0/255.0, alpha: 1)
}
}
Then you can use myColor same way as default UIColor colors.
let systemColor = UIColor.blackColor()
let myColor = UIColor.myColor()
1) Is your function returning the same color every time as in the question? In that case why don't you make it a static color in AppDelegate which you can access anywhere using
(UIApplication.sharedApplication() as AppDelegate).myColor
2) If your color will return a different color everytime based on your class properties but uses the same formula for all classes for e.g.
func myColor() -> UIColor {
return UIColor(red: (prop1 * 5)/255.0, green: prop2/255.0, blue: (prop3/2)/255.0, alpha: 1)
}
you can define prop1,prop2,prop3 and the function in a base class which every class can override and set their own values for the properties. The function will not need to be overridden.
3) If the formula for calculating the color is different for every class, you can try making a protocol which defines this function and the properties. Every class which inherits from this property will have to provide their own implementation of the formula.
You can pick the appropriate solution based on your need.
I hope this helped!
Add a swift file name it UIColorExt.swift:
import UIKit
class func RGB(r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ alpha: CGFloat = 1.0) -> UIColor{
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: alpha)
}
Usage:
view.backgroundColor = UIColor.RGB(204, 119, 70)

Resources