Dashed line border around UIView

后端 未结 23 2395
粉色の甜心
粉色の甜心 2020-12-02 04:16

How do I add dashed line border around UIView.

Something Like this

\"\"

23条回答
  •  臣服心动
    2020-12-02 04:39

    Swift 3:

    import UIKit
    
    class UIViewWithDashedLineBorder: UIView {
    
        override func draw(_ rect: CGRect) {
    
            let path = UIBezierPath(roundedRect: rect, cornerRadius: 0)
    
            UIColor.purple.setFill()
            path.fill()
    
            UIColor.orange.setStroke()
            path.lineWidth = 5
    
            let dashPattern : [CGFloat] = [10, 4]
            path.setLineDash(dashPattern, count: 2, phase: 0)
            path.stroke()
        }
    }
    

    Use in a storyboard (as custom class) or directly in code:

    let v = UIViewWithDashedLineBorder(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    

    Result:

提交回复
热议问题