iosswiftcornerradiusibinspectable

how to corner Radius some edges in IBInspectable in swift 4?


so here is the code that is working well

let path = UIBezierPath(roundedRect:viewToRound.bounds,
                    byRoundingCorners:[.topRight, .bottomLeft],
                    cornerRadii: CGSize(width: 20, height:  20))

let maskLayer = CAShapeLayer()

maskLayer.path = path.cgPath
viewToRound.layer.mask = maskLayer

But I need to use this many times in my codes So I created the class for view and want to use this in IBInspectable to be optional each edges to change the corners radiuses in story Board so I used this But it does not shown in story board

@IBInspectable
open var cornerEdges: CGSize {
    get {
        let path = UIBezierPath(roundedRect:self.bounds,
                                byRoundingCorners:[.topRight, .bottomLeft],
                                cornerRadii: CGSize(width: 20, height:  20))

        let maskLayer = CAShapeLayer()

        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
        return maskLayer.path as! CGSize
    }
    set(value) {
        maskLayer.path = value
    }
}

so what should I do for doing this in my codes ?


Solution

  • Just Change your class to @IBDesignable to see in storyboard

    Like

    @IBDesignable // Add this to your class
    class PlusButton: AnyUIClass { }
    

    You can see it in storyboard

    enter image description here

    how to Get corners and radius from storybaord

    Here is a sample code

    @IBInspectable
    open var cornerEdges: CGSize = CGSize(width: 20, height: 20)
    @IBInspectable  var topLeft: Bool = true
    @IBInspectable  var topRight: Bool = true
    @IBInspectable  var bottomLeft: Bool = true
    @IBInspectable  var bottomRight: Bool = true
    
    override func awakeFromNib() {
    
        var options = UIRectCorner()
        if topLeft {
           options =  options.union(.topLeft)
        }
        if topRight {
           options =  options.union(.topRight)
        }
        if bottomLeft {
          options =  options.union(.bottomLeft)
        }
        if bottomRight {
          options =  options.union(.bottomRight)
        }
    
    
        let path = UIBezierPath(roundedRect:self.bounds,
                                byRoundingCorners:options,
                                cornerRadii: self.cornerEdges)
    
        let maskLayer = CAShapeLayer()
    
        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
    }