Marko Zadravec Marko Zadravec - 2 months ago 11
Swift Question

Custom property of a UIView used in Storyboard

I know that could be done, but I can't remember how.

I would like to create some properties of a view and been able to change this in interface builder. Also I would like that after I specify the class of the UIView that this view is rendered correctly in interface builder.

Can someone help me

Answer

Subclass your UIView as you usually would to extend the functionality. The two unique portions of code are @IBDesignable which enables the live rendering in Interface Builder and @IBInspectable which will surface the appropriate properties in Interface Builder.

@IBDesignable class NewView: UIView {
     // Display New Color as an editable property in Interface Builder.
     @IBInspectable var newColor: UIColor = UIColor.greenColor()
}

You can then do any required drawing by overriding the drawRect(rect: CGRect) function using any newly provided properties.

Comments