Kyle Fang
Kyle Fang

Reputation: 1199

AutoLayout set the same height and top space for multiple views, without distribute them vertically

I learned that we can set equal height like this V:|-[v1]-[v2(==v1)]-[v3(==v1)]-|, but this will distribute them vertically. Which is the opposite of what I want.

Upvotes: 3

Views: 494

Answers (1)

Jesse Rusak
Jesse Rusak

Reputation: 57168

Your format string specifies a single vertical column of layout. This means you are pinning, for example, the bottom of v1 to the top of v2 (as well as the height). To avoid this, you'll need multiple constraint specifications.

First, if you want them to all have the same height, you can use two separate constraint specifications like this:

V:[v2(==v1)]
V:[v3(==v1)]

Second, you can pin all the tops with a horizontal layout format and a special option flag (thanks to @jrturton for this suggestion):

[NSLayoutConstraint constraintsWithVisualFormat:@"H:..." options:NSLayoutFormatAlignAllTop metrics:nil views:...];

Your horizontal constraint might be something like @"H:[v1]-[v2]-[v3]".

Note that simply pinning their tops together like this isn't sufficient to place them vertically; you'd also have to specify some other constraint (like V:|[v1]) to attach one of them to something else. (And, you'd also need horizontal constraints, of course.)

The easiest thing to do, though, is just to use Interface Builder, in which you can simply use the Editor menu to pin the heights and tops of these views directly.

Upvotes: 4

Related Questions