Interface Builder的全局调色板

时间:2015-07-21 22:30:36

标签: ios swift interface-builder uicolor ibinspectable

在swift中,我正在编写一个扩展(如Obj-C类),它在代码类方法中维护“房屋颜色”。我想知道是否有一种方法可以使用IBInspectable或其他东西使接口构建器可以访问此颜色扩展,而不是像我在许多IBInspector样本用法中看到的那样将颜色专门附加到UIView子类。

extension UIColor {
    // house blue
    @IBInspectable var houseBlue: UIColor { return UIColor(red:(51/255), green:(205/255), blue:(255/255), alpha:(1)) }

    // house gray
    @IBInspectable var houseGray: UIColor { return UIColor(white:0.4, alpha: 1) }

    // house white
    @IBInspectable var houseWhite: UIColor { return UIColor.whiteColor() }
}

2 个答案:

答案 0 :(得分:3)

据我所知,您无法以编程方式定义随后将在Interface Builder中显示的颜色,但您可以扩展UIColor以便在代码中使用自定义颜色:

extension UIColor {

   static func myCustomColor() -> UIColor {
     return UIColor(red: 23/255.0, green: 175/255.0, blue: 72/255.0, alpha: 1.0)
   }

}

并引用自定义颜色:

myView.backgroundColor = UIColor.myCustomColor()

这并不能完全解决您的问题,但以编程方式设置颜色可能比通过IB更好。它允许您在代码中调整一次值以更改整个应用程序的颜色,而不必通过Interface Builder为每个UI元素多次调整它。

答案 1 :(得分:1)

我发现这个问题的最佳解决方案是构建一个可以由Interface Builder使用的房屋颜色的资产目录。

https://blog.zeplin.io/asset-catalog-colors-on-xcode-9-c4fdccc0381a