If you have just a hexstring you should use this answer to convert you color.
I had the problem to handle an hexstring with 6 characters plus an alpha value as CGFloat.
So I changed the extension from https://stackoverflow.com/a/33397427/4420355 a little bit.
extension UIColor {
public convenience init?(hexString: String, alpha: CGFloat) {
var newHexString = hexString
if !hexString.hasPrefix("#") {
newHexString += "#"
}
if newHexString.count != 7 {
print("Invalid input - hex must have 6 characters: your hexstring \(newHexString) has \(newHexString.count) characters")
return nil
}
let hex = newHexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var initInt32 = UInt32()
Scanner(string: hex).scanHexInt32(&initInt32)
let r, g, b: UInt32
switch hex.count {
case 6: // RGB (24-bit)
(r, g, b) = (initInt32 >> 16, initInt32 >> 8 & 0xFF, initInt32 & 0xFF)
default:
(r, g, b) = (0, 0, 0)
}
self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: alpha)
}
}
After this you can call it like this
gmsPolygon?.strokeColor = UIColor(hexString: "#aaaaaa", withAlpha: 0.2)
gmsPolygon?.strokeColor = UIColor(hexString: "aaaaaa", withAlpha: 0.2)