0
votes

I'm trying to figure out how to center vertically a view using Auto-Layout. I'm new to this technology so it makes me some problems... This is my controller in Interface Builder: enter image description here

The gray part is the superview and it contains a custom view (the yellow one) and a label (the red one); the yellow view is fixed at the bottom of the superview and it has fixe height and width. The red view has fixed width and height, too.

My goal is to center vertically my red view in the visible part of the gray view, whose visible height is superview.height - yellowView.height. How can I do that?
Thank you so much!

2
Do you need the red view to stay entered on different size screens, and on rotation?rdelmar
Different screen sizesmatteodv

2 Answers

2
votes

The easiest way to do it, would be to make the gray view a subview of the main view too, and then you can just give the label a centerY constraint in IB or in code. If you can't so that for some reason, you can change the constant value of a centerY constraint in code, giving it a value of 1/2 the height of the yellow view. Give the label a centerY constraint in IB and make an IBOutlet to it (I call it centerCon in my example). The fixed height of my yellow view was 200.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.centerCon.constant = 100;
}
-3
votes

One of the simplest solutions here will be like this:

redView.center = CGPointMake(superview.width/2, (superview.height - yellowView.height)/2);