How to use IBInspectable in Swift 3.0

@IBInspectable available from iOS 8.0.  The @IBInspectable properties provide the new class which is old feature of User define runtime attribute. This is powerful mechanisms apply for XIB , NIB or StoryBoard. Here we apply IBInspectable property for custom UIView. Here we study steps with @IBInspectable property on custom view.


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 How to use IBInspectable in Swift 3.0 Reviewed by KIRIT MODI on 09:50:00 Rating: 5

No comments:

Powered by Blogger.