Chris
Chris

Reputation: 27384

Centering a child UIView to its parent UIView

I want to center a subview in the middle of its parent, I have looked at answers on SO but so far they have not helped me. This one specifically looks like it should work, but doesn't.

Here is what I am doing

-(void)viewDidLoad {
    [super viewDidLoad];

    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
    finalNumberCircle.Color = [UIColor darkGrayColor];
    [self.view addSubview:finalNumberCircle];
}

I also tried the following:

-(void)viewDidLoad {
    [super viewDidLoad];

    CGPoint translatedP = [self.view convertPoint:self.view.center fromView:self.view.superview];
    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100];
    finalNumberCircle.center = translatedP;
    [self.view addSubView:finalNumberCircle];
}

Here is how it looks (it's the grey circle)

enter image description here

Upvotes: 0

Views: 398

Answers (5)

LuisEspinoza
LuisEspinoza

Reputation: 8538

I think that you should move half width to the left, and half height upwards:

-(void)viewDidLoad {
    [super viewDidLoad];
    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.center.x-50, self.view.center.y-50, 100, 100]; 
    [self.view addSubView:finalNumberCircle];
}

But I would use something like:

#DEFINE CIRCLE_RADIUS    50

-(void)viewDidLoad {
    [super viewDidLoad];
    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.center.x-CIRCLE_RADIUS, self.view.center.y-CIRCLE_RADIUS, 2*CIRCLE_RADIUS, 2*CIRCLE_RADIUS]; 
    [self.view addSubView:finalNumberCircle];
}

I uploaded a demo https://github.com/luisespinoza/CenterViewTest

Upvotes: 0

peko
peko

Reputation: 11335

You have to move your code (2. example) from viewDidLoad to viewWillLayoutSubviews and it will work as suspected.

Before viewWillLayoutSubview is called your viewcontroller view bounds still can change.

Upvotes: 1

Guntis Treulands
Guntis Treulands

Reputation: 4762

Another possibility how to do it (in case You need specific offset from sides). Just provide an offset from parent view sides, and it will auto-resize itself in center of parent view, and will also autoresize, in case parent view frame is changed.

- (void)viewDidLoad 
{
    [super viewDidLoad];

    CGFloat mOffsetFromSide = 50;

    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x+mOffsetFromSide, self.view.bounds.origin.y+mOffsetFromSide, self.view.bounds.size.width-mOffsetFromSide*2, self.view.bounds.size.height-mOffsetFromSide*2];

    [finalNumberCircle setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];

    [self.view addSubView:finalNumberCircle];
}

In case You simply want to position view in it's parent view's center, leaving it's frame intact, then:

finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);

[self.view addSubview:finalNumberCircle]; 

And yes - it is exact copy of Your provided code. It works on my side.

Maybe problem is with CircleView itself. Or You change it's frame later in code?

What happens if You change "CircleView" to simply UIView ?

Upvotes: 0

Raon
Raon

Reputation: 1286

Try below code...

[self.view addSubView:finalNumberCircle];

finalNumberCircle.frame=CGRectMake(finalNumberCircle.frame.origin.x+(self.view.frame.size.width/2-finalNumberCircle.center.x), finalNumberCircle.frame.origin.y+(self.view.frame.size.height/2-finalNumberCircle.center.y), finalNumberCircle.frame.size.width, finalNumberCircle.frame.size.height);

Upvotes: 0

in.disee
in.disee

Reputation: 1112

convertPoint and related methods used when you want to convert coordinates from one view's coordinates to another, that's not your situation. You have to do something like:

finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
[self.view addSubview:finalNumberCircle];

Upvotes: 0

Related Questions