How to access extension of UIColor in Swift?

后端 未结 9 1482
猫巷女王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 01:53

    Could use a computed property:

    extension UIColor {
      var customBlueColor: UIColor {
        return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
      }
    }
    

    And then to call it:

    UIColor().customBlueColor
    
    0 讨论(0)
  • 2020-12-10 01:57

    Swift:

    extension UIColor {
        open class var yourOrange: UIColor {
            return UIColor.init(colorLiteralRed: 0.988, green: 0.337, blue: 0.063, alpha: 1)
        }
    } 
    
    0 讨论(0)
  • 2020-12-10 01:59

    You just need to change your statement like,

     btnShare.setTitleColor(UIColor().getCustomBlueColor(), forState:.Normal)
    

    More detailed explanation is here.

    0 讨论(0)
  • 2020-12-10 02:04

    UIColor Extension Swift 5

    extension UIColor {
        static var yourColor:UIColor {
            return UIColor(red: 0.745, green: 0.157, blue: 0.074, alpha: 1)
        }
    }
    

    Use : view.backgroundColor = .yourColor

    0 讨论(0)
  • 2020-12-10 02:08

    You have defined an instance method, which means that you can call it only on an UIColor instance:

    let col = UIColor().getCustomBlueColor()
    // or in your case:
    btnShare.setTitleColor(UIColor().getCustomBlueColor(), forState: .Normal)
    

    The compiler error "missing argument" occurs because Instance Methods are Curried Functions in Swift, so it could equivalently be called as

    let col = UIColor.getCustomBlueColor(UIColor())()
    

    (But that would be a strange thing to do, and I have added it only to explain where the error message comes from.)


    But what you really want is a type method (class func)

    extension UIColor{
        class func getCustomBlueColor() -> UIColor{
            return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
        }
    }
    

    which is called as

    let col = UIColor.getCustomBlueColor()
    // or in your case:
    btnShare.setTitleColor(UIColor.getCustomBlueColor(), forState: .Normal)
    

    without the need to create an UIColor instance first.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题