I find using hex codes file defining colours much easier than rgba, so I researched about how to use a converter from hex code to UIColor. I first created a new file called UIColorUtility
and added:
import Foundation
import UIKit
extension UIColor {
public convenience init?(hexString: String) {
let r, g, b, a: CGFloat
if hexString.hasPrefix("#") {
let start = hexString.startIndex.advancedBy(1)
let hexColor = hexString.substringFromIndex(start)
if hexColor.characters.count == 8 {
let scanner = NSScanner(string: hexColor)
var hexNumber: UInt64 = 0
if scanner.scanHexLongLong(&hexNumber) {
r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
a = CGFloat(hexNumber & 0x000000ff) / 255
self.init(red: r, green: g, blue: b, alpha: a)
return
}
}
}
return nil
}
}
Then in my viewController, I tried to change this bit
.SelectionIndicatorColor(UIColor(red: 0.8, green: 0.153, blue: 0.106, alpha: 1.0)),
into this bid:
.SelectionIndicatorColor(UIColor(hexString: "#929292"))!,
It first forced me to unwrap it, and then started giving me error..
Type of expression is ambiguous without more context
What am I doing wrong? Also, just out of curiosity, how can I set the alpha too after applying hexString approach?