Like, CGRectMake()
, but have it specify the center, both horizontally and vertically?
Asked
Active
Viewed 378 times
0

Doug Smith
- 29,668
- 57
- 204
- 388
-
You want the frame to be the same size as the superview, then center it? http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel?rq=1 – rog May 19 '13 at 20:33
3 Answers
3
- Get the dimensions of the containing view.
- Get the dimensions of the view to be inserted.
- x_position = width_of_containing_view / 2.0 - width_of_inserted_view / 2.0
- y_position = height_of_containing_view / 2.0 - height_of_inserted_view / 2.0

Hot Licks
- 47,103
- 17
- 93
- 151
0
You can also do label.center = [superview convertPoint:superview.center fromView:superview.superview];
=P

fumoboy007
- 5,345
- 4
- 32
- 49
0
Set the label's center
property to the center point of it's superview's bounds
. Since the superview's center
and frame
properties are in coordinate space of their superview, those properties aren't helpful (unless you do the conversion like fumoboy does).
label.center = CGRectGetMidPoint(label.superview.bounds);
I made this function for convenience:
CGPoint CGRectGetMidPoint(CGRect rect)
{
return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}

MaxGabriel
- 7,617
- 4
- 35
- 82