简体   繁体   中英

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   
                } 
            }
        }

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM