Search code examples
iosswiftuiviewcashapelayer

dotted line is not coming till the end in uiview


I am trying to create a dotted line, I have tried with the code below:

 private func createDottedView(for view:UIView) {
        let width: CGFloat = 1
        let color: CGColor = UIColor.black.cgColor

        let caShapeLayer = CAShapeLayer()
        caShapeLayer.strokeColor = color
        caShapeLayer.lineWidth = width
        caShapeLayer.lineDashPattern = [9,3]
        let cgPath = CGMutablePath()
        let cgPoint = [CGPoint(x: view.layer.bounds.minX, y: 0), CGPoint(x: view.layer.bounds.maxX, y: 0)]
        cgPath.addLines(between: cgPoint)
        caShapeLayer.path = cgPath
        view.layer.addSublayer(caShapeLayer)
    }

But this is not coming till end, how can i get this dotted line till end?

this is what i am getting


Solution

  • You can use an image misted of making a uiview a dotted line because of the CAShapeLayer() It can cause this type of problem. So you can just download a similar image and place the image view inside the uiview and use the image to make it scale to fill it should work.

    Or if you want to do it with coding only you can change your code with the given live of code the rest of the code will work fine.

    let cgPoint =  [CGPoint(x: view.layer.bounds.minX, y: 0), CGPoint(x: 
    view.layer.bounds.maxX+40, y: 0)]