How to access extension of UIColor in Swift?

后端 未结 9 1484
猫巷女王i
猫巷女王i 2020-12-10 01:15

I am very new to swift and trying to create an extension of UIColor class as

extension UIColor{

    func getCustomBlueColor() -> UIColor {
        retur         


        
9条回答
  •  北海茫月
    2020-12-10 02:09

    Swift 3, Swift 4, Swift 5:

    extension UIColor {
       static let myBlue = UIColor(red:0.043, green:0.576 ,blue:0.588, alpha:1.00) 
    }
    

    Use:

    btnShare.setTitleColor(.myBlue, for: .normal)
    

    Or

    self.view.backgroundColor = .myBlue
    

    If you use Color Set in *.xcassets (iOS11+). For example, you have a color with the name «appBlue». Then:

    extension UIColor {
    
    private static func getColorForName(_ colorName: String) -> UIColor {
        UIColor(named: colorName) ?? UIColor.red
    }
    
    static var appBlue: UIColor {
        self.getColorForName("appBlue")
    }
    }
    

    Use:

    self.view.backgroundColor = .appBlue
    

提交回复
热议问题