I created a CAShapeLayer and added it as a sublayer instead of working directly on view.layer to allow a dotted or dashed border (using lineDashPattern). The issue is that at some border widths and with some corner radius values - random vertical lines begin appearing. Zoom in (if you have to) on the picture, on the right-hand side.
Note: the lines are vertical on my physical test device but horizontal in simulator.
Click to see screenshot of test project
I've created a small test project to demonstrate this. In storyboard I setup the red square and slider and all the border work is done at the bottom of this code snippet (I've included everything in the class for clarity):
@IBOutlet weak var testView: UIView!
@IBOutlet weak var slider: UISlider!
var border : CAShapeLayer?
override func viewDidLoad() {
super.viewDidLoad()
updateViewCornerRadius(newCornerRadius: 0.0)
slider.addTarget(self, action: #selector(ViewController.onSliderChange), for: UIControlEvents.valueChanged)
}
func onSliderChange() {
updateViewCornerRadius(newCornerRadius: slider.value)
}
func updateViewCornerRadius(newCornerRadius : Float) {
if border != nil {
border?.removeFromSuperlayer()
}
let cornerRadius = CGFloat(newCornerRadius)
testView.layer.cornerRadius = cornerRadius
border = CAShapeLayer()
border!.masksToBounds = true
border!.cornerRadius = cornerRadius
border!.strokeColor = UIColor.black.cgColor
border!.lineDashPattern = [1, 0]
border!.frame = testView.bounds
border!.fillColor = UIColor.clear.cgColor
border!.path = UIBezierPath(roundedRect: testView.bounds, byRoundingCorners: UIRectCorner.allCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)).cgPath
border!.lineWidth = 12
testView.layer.addSublayer(border!)
}
Has anybody ever dealt with this issue? Is there a setting on the UIView or CAShapeLayer I could set to get rid of it?