Logan Gallois
Logan Gallois

Reputation: 622

iOS 9 center position of path change with size screen

I would like to add a path in an UIView and i am doing like this :

let Circle = UIBezierPath(arcCenter: CGPoint(x: InterfaceView.bounds.size.width/2, y: InterfaceView.bounds.size.height/2), radius: 10, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
let Layer = CAShapeLayer()
Layer.path = Circle.CGPath
Layer.strokeColor = UIColor.redColor().CGColor
Layer.lineWidth = 1.0
InterfaceView.layer.addSublayer(Layer)

That's work on iPhone 6 and 6S but when the screen is tiny or bigger, the circle doesn't have a good position :

iPhone 6S :

enter image description here

iPhone 5S :

enter image description here

Maybe an idea why ?

Thanks

Upvotes: 1

Views: 1269

Answers (2)

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

Probably you should use center parameter. I test this on different screen sizes and circle is directly on the middle of screen.

 let Circle = UIBezierPath(arcCenter: self.view.center, radius: 10, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
        let Layer = CAShapeLayer()
        Layer.path = Circle.CGPath
        Layer.strokeColor = UIColor.redColor().CGColor
        Layer.lineWidth = 1.0
        self.view.layer.addSublayer(Layer)

Upvotes: 0

TheEye
TheEye

Reputation: 9346

You probably draw the path at a moment where the autolayout constraints didn't kick in yet, so the drawing is made relative to the design-time view frame.

Two possible solutions:

1) Draw the circle in a subview that is centered in the full view with autolayout constraints and always has the same size.

2) Redraw your path on any size change in viewDidLayoutSubviews.

Upvotes: 1

Related Questions