I have a UILabel
that is placed on my Storyboard with AutoLayout. (Constraints include height, width and center horizontally, and vertical spacing from the top).
I have UIView
that makes a circle with the drawRect
method using a UIBeizerPath
.
Nothing fancy so far.
For simplicity sake I am using hard coded numbers to help illustrate my problem.
Now I want the CircleView to be placed over the UILabel
, centering the label in the circle.
However, I can not get anything to line up correctly. Its like the anchor points or something is messing up.
I tried setting the CircleView’s centerpoint to the labels center point. No luck.
I tried setting the CircleViews X to the location of the label minus the width divided by two. No luck.
The closest I can get is with the Y coordinate. I use the label.center.y – 52.5 (half of the radius).
cv = [[CircleView alloc] initWithFrame:CGRectMake(WHATHERE.x, WHATHERE.y,
155, 155)];
cv.radius = 75;
cv.strokeWidth = 5;
The radius of the circle is 75. The width/height of the CircleView is 155 because the stroke of the circle is 5. (radius x 2) + 5, which give me the view you see with the circle.
The dark background is the view of the iOS simulator. I have added background colors to each of the elements to distinguish their size.
Through the magic of Photoshop here is what I am trying to accomplish:
3
Answers
Here’s what I would do:
set the circle view’s frame to
Do The init of circle in
viewDidAppear
instead ofviewDidLoad
Then you’re doing something wrong… Use constraints !
Here is my Storyboard label constraints
Here my ViewController
Here’s CircleView
And here’s the result
Piece of cake ! 😉