ViewController.swift
//Hexagon color enhancement
extension UIColor {
convenience init(hex: String, alpha: CGFloat = 1.0) {
let v = Int("000000" + hex, radix: 16) ?? 0
let r = CGFloat(v / Int(powf(256, 2)) % 256) / 255
let g = CGFloat(v / Int(powf(256, 1)) % 256) / 255
let b = CGFloat(v / Int(powf(256, 0)) % 256) / 255
self.init(red: r, green: g, blue: b, alpha: min(max(alpha, 0), 1))
}
}
First, write the above code. The color specification method after that is as follows.
ViewController.swift
//Example) When you want to change the color of tabbar
tabBar.barTintColor = UIColor(hex: "2DCCD3")
Describe the hexadecimal number as string type
.
Recommended Posts