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

前端 未结 6 2093
北海茫月
北海茫月 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:14

    Swift 5, 4

    iOS 11

    override func draw(_ rect: CGRect) {
      super.draw(rect)
    
      guard let context = UIGraphicsGetCurrentContext() else { return }
    
      context.saveGState()
      defer { context.restoreGState() }
    
      let path = UIBezierPath(
        roundedRect: rect,
        byRoundingCorners: [.topLeft, .topRight],
        cornerRadii: CGSize(width: 4, height: 4)
      )
    
      context.addPath(path.cgPath)
      context.closePath()
    
      context.setStrokeColor(UIColor.red.cgColor)
      context.strokePath()
    }
    
    

提交回复
热议问题