How can I make a Swift enum with UIColor value?

前端 未结 9 733
深忆病人
深忆病人 2020-12-16 09:37

I\'m making a drawing app and I would like to refer to my colors through use of an enum. For example, it would be cleaner and more convenient to use Colors.RedColor

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 09:59

    I use computed properties to solve this problem, this is my code

    enum MyColor {
        case navigationBarBackgroundColor
        case navigationTintCololr
    }
    
    extension MyColor {
        var value: UIColor {
            get {
                switch self {
                case .navigationBarBackgroundColor:
                    return UIColor(red: 67/255, green: 173/255, blue: 247/255, alpha: 1.0)
                case .navigationTintCololr:
                    return UIColor.white
                }
            }
        }
    }
    

    then I can use MyColor like this:

    MyColor.navigationBarBackgroundColor.value
    

提交回复
热议问题