1

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 ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60

2 Answers2

3

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
}
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
1

so if you want to change both height and width edge corner you can use @PrashantTukadiya answer But if you want to just set a number for corner you can use this code

 @IBInspectable
open var cornerEdges : CGFloat = 0
@IBInspectable  var topLeft: Bool = false
@IBInspectable  var topRight: Bool = false
@IBInspectable  var bottomLeft: Bool = false
@IBInspectable  var bottomRight: Bool = false
 override func awakeFromNib() {
    super.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: CGSize(width: self.cornerEdges, height: self.cornerEdges))

    let maskLayer = CAShapeLayer()

    maskLayer.path = path.cgPath
    self.layer.mask = maskLayer
}
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60