how to change ui color to color set

Viewed 217

can someone please tell me if I can change the color on below format using Color Set. For example, Instead of .blue, I want to use my own color set such as Color("colorname"). Is this possible

.onAppear {
UITableView.appearance().backgroundColor = .blue
}
3 Answers

UITableView.appearance().backgroundColor = UIColor(named: "your set here")

You can create your own color like this:

extension UIColor {
     struct rickyPurpleColor {
     static let normal = UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
    static let light = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
    static let dark = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
    }
    }

After you can use it like this:

        .onAppear {
    UITableView.appearance().backgroundColor = Color(UIColor.rickyPurpleColor.normal)
    }

You could write a static extension for UIColor, like this:

extension UIColor {
    public static let yourColor = // ... define any color here
}

You can then access this value from anywhere within your app just like the pre-defined colors (e.g. .blue, but instead of blue you use your custom name).

Related