Global Color Palette for Interface Builder
Asked Answered
A

2

11

In swift i'm writing an extension (like Obj-C category) that maintains in code class methods of "house colors." I'm wondering if there is a way to make this color extension accessible to Interface builder using IBInspectable or something else as opposed to attaching colors specifically to UIView subclasses as i've seen in a lot of IBInspector sample usage.

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() }
}
Anora answered 21/7, 2015 at 22:30 Comment(3)
This would be awesome! But it's not possible of my knowledge. You can however add a custom color palette inside the Interface Builder, but I think they're only available for usage in there.Outgoing
Are you trying to only use it in storyboards but not in your actual code like UIView.backgroundColor = x?Cascade
Both, really. @CodyWeaverAnora
A
2

The best solution I have found to this problem is to build an asset catalog of house colors, which can be used by Interface Builder.

https://help.apple.com/xcode/mac/current/#/dev10510b1f7

Anora answered 7/5, 2018 at 18:56 Comment(0)
H
3

As far as I know, you can't programatically define colors that will then show up in Interface Builder, but you can extend UIColor for custom colors to use in your code:

extension UIColor {

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

}

And to reference the custom color:

myView.backgroundColor = UIColor.myCustomColor()

This doesn't completely solve your issue, but it could potentially be better to set colors programatically than through IB. It would allow you to adjust a value one time in code to change a color throughout the app rather than having to adjust it multiple times for each UI element through Interface Builder.

Hereon answered 18/7, 2016 at 3:30 Comment(0)
A
2

The best solution I have found to this problem is to build an asset catalog of house colors, which can be used by Interface Builder.

https://help.apple.com/xcode/mac/current/#/dev10510b1f7

Anora answered 7/5, 2018 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.