Q:

How to reuse the same IBDesignable code in UIView and UIImageView

 

I have a IBDesignable class for making border line and corner radius.
  The problem is I have to write separate classes for UIView and
  UIImageview. Is there a simple solution for this. Thanks in advance.

 @IBDesignable
    class View: UIView {
        //MARK:- Corner Radius
        @IBInspectable
        var cornerRadius : CGFloat =  0{
          didSet {
                self.layer.masksToBounds = true
                self.layer.cornerRadius = cornerRadius   
            } 
        }
    }

Image view

 @IBDesignable
        class ImageView: UIImageView {
            //MARK:- Corner Radius
            @IBInspectable
            var cornerRadius : CGFloat =  0{
              didSet {
                    self.layer.masksToBounds = true
                    self.layer.cornerRadius = cornerRadius   
                } 
            }
        }

A:

Create extension like below
extension UIView
{
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius;
        }
        set(value) {
            layer.cornerRadius = value;
        }
    }

    var rounded: Bool {
        return layer.cornerRadius > 0.0;
    }
}

