Dashed line border around UIView

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

How do I add dashed line border around UIView.

Something Like this

\"\"

23条回答
  •  不知归路
    2020-12-02 04:45

    Swift version of the QuartzCore answer.

    import QuartzCore    
    
    let dottedPattern = UIImage(named: "dottedPattern")
    myView.layer.borderWidth = 1
    myView.layer.borderColor = UIColor(patternImage: dottedPattern!).CGColor
    

    The CAShapeLayer approach works, but the QuartzCore approach is better at handling a Table View reload, if the UIView is inside a cell.

    For the image, you can use something like this (it's really small):

    I tend to prefer vector over PNGs when I can get away with it:

    • Within Sketch, create a 4x4 pixel rectangle.
    • Make a total of four of these
    • Group them into a foursquare, alternating colors
    • Export the group as PDF
    • Within Images.xcassets, create a New Image Set called dottedPattern
    • Change the Scale Factors to Single Vector
    • Drop in your PDF

提交回复
热议问题