Dashed line border around UIView

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

How do I add dashed line border around UIView.

Something Like this

\"\"

23条回答
  •  悲哀的现实
    2020-12-02 04:40

    You can simply create a IBDesignable class like this:

    import UIKit
    
    @IBDesignable
    class BorderedView: UIView {
    
        @IBInspectable var cornerRadius: CGFloat = 0
    
        @IBInspectable var borderWidth: CGFloat = 0
    
        @IBInspectable var borderColor: UIColor = UIColor.clear
    
        override func draw(_ rect: CGRect) {
            let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
            path.lineWidth = borderWidth
    
            borderColor.setStroke()
    
            let dashPattern : [CGFloat] = [10, 4]
            path.setLineDash(dashPattern, count: 2, phase: 0)
            path.stroke()
        }
    
    }
    

    Then just subclass your view with BorderedView from Xcode. This way you can set the border color and border width very easily from the interface builder!

提交回复
热议问题