Reputation: 1206
I have a view that contains three labels.
The first label is placed at the very top. The third label is placed 50px from the bottom.
What I am trying to do is always place the second label in the center of those two using autolayout, but I can't figure how to do it.
The problem is that the second label isn't at the view's center.
I tried setting two vertical spacing constrains to less or equal to the initial value, but it did not work.
It is possible to do that only using autolayout? I thought about adding another view, but that does not look like a good solution..
Thank you.
Upvotes: 5
Views: 849
Reputation: 5683
My other answer I think was incorrect. I think the easiest way to do this is to create a NSView
that holds the labels and the second label is set to be in the centre of it. My previous answer assumed that when the Autolayout system needed to split space between views it would do it equally, but some quick tests suggest that it doesn't.
I don't think it's possible to say that a certain space should be equal to another space at the moment with Autolayout.
Upvotes: 0
Reputation: 5553
I don't think you can do this with auto layout; but it's trivial to override layoutSubviews
(or viewDidLayoutSubviews
if you're not using a UIView
subclass) to center label 2 between label 1 and label 3 (just be sure to call [super layoutSubviews]
first).
EDIT: Here's some sample code.
- (void)layoutSubviews {
[super layoutSubviews];
_label2.center = CGPointMake(_label2.center.x, (CGRectGetMaxY(_label1.frame) + CGRectGetMinY(_label3.frame)) / 2);
}
Upvotes: 1