I'm having trouble centering CAShapeLayer within a UIView. I've search and most solutions are from pre 2015 in Obj C or haven't been solved.
Attached is what the image looks like. When I inspect it, its inside the red view, but idk why its not centering. I've tried resizing it but still doesn't work.
let progressView: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
//MARK: - ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(progressView)
progressView.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 200, height: 200)
progressView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
progressView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
setupCircleLayers()
}
var shapeLayer: CAShapeLayer!
private func setupCircleLayers() {
let trackLayer = createCircleShapeLayer(strokeColor: UIColor.rgb(red: 56, green: 25, blue: 49, alpha: 1), fillColor: #colorLiteral(red: 0.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1))
progressView.layer.addSublayer(trackLayer)
}
private func createCircleShapeLayer(strokeColor: UIColor, fillColor: UIColor) -> CAShapeLayer {
let centerpoint = CGPoint(x: progressView.frame.width / 2, y: progressView.frame.height / 2)
let circularPath = UIBezierPath(arcCenter: centerpoint, radius: 100, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
let layer = CAShapeLayer()
layer.path = circularPath.cgPath
layer.fillColor = fillColor.cgColor
layer.lineCap = kCALineCapRound
layer.position = progressView.center
return layer
}
As @ukim says, your problem is that you are trying to determine the position of your layer, based on views and their size before these are finite.
When you are in
viewDidLoad
you don't know the size and final position of your views yet. You can add theprogressView
alright but you can not be sure that its size or position are correct untilviewDidLayoutSubviews
(documented here).So, if I move your call to
setupCircleLayers
toviewDidLayoutSubviews
and I change the centerpoint toCGPoint.zero
and alter the calculation of yourlayer.position
to this:Then I see this:
Which I hope is more what you were aiming for.
Here is the complete listing (note that I had to change some of your methods as I didn't have access to
anchor
orUIColor.rgb
for instance but you can probably work your way around that :))Hope that helps.
Caveat
When you do the above, that also means that every time
viewDidLayoutSubviews
is called, you are adding a new layer. To circumvent that, you can use thename
property of a layerand then check if you have already added your layer. Something like this should work:
Which you then use here:
I've updated the listing.
You are calling
setupCircleLayers()
inviewDidLoad()
. At the time,progressView.frame
has not been calculated from the constraints yet.Try
instead of calculating the value from
progressView.frame
You can try this in your playground: