How to draw a simple rounded rect in swift (rounded corners)

前端 未结 6 2111
北海茫月
北海茫月 2020-12-25 14:39

I managed to draw a rect :-) But I don\'t know how to draw a rounded rect.

Can someone help me out with the following code how to round the rect?

let         


        
6条回答
  •  难免孤独
    2020-12-25 15:10

    @muku answer in Swift 4.x+:

    override func draw(_ rect: CGRect) {
      super.draw(rect)
    
      let clipPath = UIBezierPath(roundedRect: rect, cornerRadius: 6.0).cgPath
    
      let ctx = UIGraphicsGetCurrentContext()!
      ctx.addPath(clipPath)
      ctx.setFillColor(UIColor.red.cgColor)
    
      ctx.closePath()
      ctx.fillPath()
    }
    

提交回复
热议问题