Reputation: 4675
I am finally trying to adopt autolayout in IB and I am having trouble setting the constraints for some objects. I basically want 5 views to be equally distributed vertically throughout the superview. I have 3 buttons separated by 2 lines. I want the spacings D1, D2, D3, and D4 to be equal WITHOUT resizing the height of anything. How can this be set up in IB?
Upvotes: 4
Views: 1840
Reputation: 437622
You can create UILayoutGuide
objects (or, for iOS versions before iOS 9, UIView
objects) for D1, D2, D3, and D4, e.g., spacer1
, spacer2
, etc., and then define their heights to be equal to each over. In Visual Format Language (VFL), the vertical constraint would look something like:
@"V:|-[button1][spacer1][separator1(4)][spacer2(==spacer1)][button2][spacer3(==spacer1)][separator2(4)][spacer4(==spacer1)][button3]-|"
I don't know how you're representing your separator lines (here I'm just using a 4 point tall UIView
, but you can do it any way you want). But hopefully this illustrates the idea.
If you were doing this in Interface Builder, you can add UIView
objects in between the visible controls, and then select all of the spacer views (e.g. by shift-clicking) and then add a constraint to pin the "Heights Equally" to make them all the same height.
You can achieve a similar effect programmatically without the spacer views by creating a constraint on the NSLayoutAttributeCenterY
attribute of each button and separator line and adjusting the multiplier
value for each such that they're evenly spaced. The effect is very similar, but not identical.
Upvotes: 1