How to use IBInspectable in Swift 3.0
Step 1 : Create a class InspectableView Parents of UIView. In which you set the variable of color, corner Radius, border Width and color with IBInspectable .
class InspectableView: UIView {
// Background Color.
@IBInspectable var backColor: UIColor? {
didSet {
backgroundColor = backColor
}
}
// Corner Radius.
@IBInspectable var corRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = corRadius
layer.masksToBounds = corRadius > 0
}
}
// Border Width
@IBInspectable var borderwidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderwidth
layer.masksToBounds = borderwidth > 0
}
}
// Border Color.
@IBInspectable var borderColor : UIColor?{
didSet{
layer.borderColor = borderColor?.cgColor
}
}
}
Step 2 : Take one view in story Board and give the class name which is define as above in step 1, put the class in UIView shown as below image.
Step 3 : Whenever you give the parents class as IBInspectableView then its defined properties are display in the property inspection. Below image you can see where user can easily change its value.
Step 2 : After set of the all property, Run the App. You get below output.
Thanks.
How to use IBInspectable in Swift 3.0
Reviewed by KIRIT MODI
on
09:50:00
Rating:
No comments: