Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 475
Marko Zadravec Avatar asked Sep 16 '16 15:09

Marko Zadravec


People also ask

How do I add a custom view to storyboard?

Using a custom view in storyboardsOpen up your story board and drag a View (colored orange below for visibility) from the Object Library into your view controller. Set the view's custom class to your custom view's class. Create an outlet for the custom view in your view controller.

What is UIView?

The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.

How is SwiftUI different from storyboard?

Storyboard Advantages over SwiftUIStoryboard consist of Xib and custom View which can be reusable in the projects. It is easier to build an app as there is only drag and drop of the elements of the storyboard. It consist of use number of developer who are used to Swift Storyboard.


1 Answers

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.

like image 127
Hamer Avatar answered Oct 19 '22 22:10

Hamer