如何在 Swift 中访问 UIColor 的扩展?

How to access extension of UIColor in Swift?

我是 swift 的新手,正在尝试创建 UIColor class 的扩展作为

extension UIColor{

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

}

在此之后我访问了该方法

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

我不知道应该将什么作为参数传递给此语句。

你只需要改变你的陈述,

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

更详细的解释是here.

您已经定义了一个实例方法,这意味着您可以调用 它仅在 UIColor 个实例上:

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

编译器错误"missing argument"是因为 Instance Methods are Curried Functions in Swift, 所以它可以等效地称为

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

(但这将是一件奇怪的事情,我只将它添加到 解释错误消息的来源。)


但你真正想要的是一个类型方法 (class func)

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

被称为

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

无需先创建 UIColor 实例。

您定义了一个实例函数。这意味着你需要一个 UIColor 的实例以防使用 getCustomBlueColor()-method.

您似乎想要一个 class 方法,而不是实例方法。所以你必须像这样改变你的定义:

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

注意 func 之前的 'class',因此该方法现在可以作为 class 方法访问。

同一故事在结构中使用 class 方法:

struct MyColors{
    static func getCustomBlueColor() -> UIColor{
        return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
    }
}
let color = MyColors.getCustomBlueColor()

如果您只想拥有一个带有一些颜色定义的 class,我建议您在 class 或扩展名上使用结构:

struct MyColors{
    static var getCustomBlueColor = { return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) }
}
let color = MyColors.getCustomBlueColor()

可以使用计算 属性:

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

然后调用它:

UIColor().customBlueColor

使用 Swift 3,相应地使用预定义的 UIColors:

var myColor: UIColor = .white // or .clear or whatever

因此,如果您想要类似的东西,例如下面...

var myColor: UIColor = .myCustomColor

...然后,您将像这样定义扩展名:

extension UIColor {

    public class var myCustomColor: UIColor {
        return UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
    }

}

事实上,Apple 将白色定义为:

public class var white: UIColor

Swift3,Swift4,Swift5:

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

使用:

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

self.view.backgroundColor = .myBlue

如果您使用 *.xcassets 中的颜色集 (iOS11+)。 例如,您有一种名为 «appBlue» 的颜色。那么:

extension UIColor {

private static func getColorForName(_ colorName: String) -> UIColor {
    UIColor(named: colorName) ?? UIColor.red
}

static var appBlue: UIColor {
    self.getColorForName("appBlue")
}
}

使用:

self.view.backgroundColor = .appBlue

Swift:

extension UIColor {
    open class var yourOrange: UIColor {
        return UIColor.init(colorLiteralRed: 0.988, green: 0.337, blue: 0.063, alpha: 1)
    }
} 

获取自定义类型 UIView 的扩展

extension UIColor {
    // Method returns a custom color
    static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
        return .init(red: blue / 255, green: green / 255, blue: blue / 255, alpha: 1.0)
    }
}

UIColor 扩展 Swift 5

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

使用: view.backgroundColor = .yourColor