[As of Xcode 9.2] This is an unfortunate workaround, but for now I'm using IB-specific stand-in colors. It's not perfect, but we can at least see something while designing in Interface Builder.
@IBDesignable
class MyView: UIView {
@IBInspectable
var addCoolSubview: Bool = false {
didSet {
if self.addCoolSubview {
let coolView = CoolView() // Some custom view with a `coolColor` property.
#if TARGET_INTERFACE_BUILDER
coolView.coolColor = UIColor.blue // An IB-only stand-in color.
#else
coolView.coolColor = UIColor(named: "myCoolBlue") // The run-time color we really want.
#endif
self.addSubview(coolView)
}
}
}
}
Every UIView
also has a prepareForInterfaceBuilder()
method, which runs only when building for Interface Builder, that could be overridden to similar effect.
@IBDesignable
class MyOtherView: UIView {
var myWarmColor: UIColor? = UIColor(named: "myWarmColor") // Will be `nil` in IB.
// ... view does something with `myWarmColor` that would be visible in IB ...
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
// Set some IB-only stand-in colors here:
self.myWarmColor = UIColor.orange
}
}
With regard to choosing named colors in @IBInspectable
properties in Interface Builder, it's only half-broken. As a workaround, you can choose a named color from the Recently Used Colors section, if you can pick it out of the group by sight. When you pick a named color, it will properly display the name, but append (missing)
for whatever reason. Just ignore the (missing)
; it will display properly in IB and at run-time.
One other point of note is that Xcode (as of 9.2) might try to add the named color again to your Storyboard file, so you'll have to check the XML to see if the color is defined there multiple times (color definitions are near the bottom). Or you could just roll with, and eventually Xcode will notice and fix it by itself, blaming the duplicate color definition on SCM.
String
IBInspectable
property such ascolourName
which you then use to set your custom colours? – StertorousIBInspectable
property in InterfaceBuilder. – Nunciature