arsenius
arsenius

Reputation: 13246

Autolayout - Match height, not heights (Unidirectional)

Is it possible to have a unidirectional size match using autolayout and interface builder?

For example, I might have two labels. I don't want label A to be larger than label B, and I want B to have its intrinsic size. But using "match heights/widths" could result in a large amount of text increasing A's size, and therefore B's.

Upvotes: 0

Views: 67

Answers (1)

Fogmeister
Fogmeister

Reputation: 77621

The way to do this would be to have two constraints.

  1. An equal heights constraint between the label and the image view.
  2. A height constraint on the image view.

This will first set the height of the image view with the fixed height constraint and then will set the height of the label from the height of the image view (equal heights).

By doing this the label will not grow with the amount of text it has. Its height is effectively fixed by the image view.

It will not make the image view get any bigger as this would contradict the fixed height.

EDIT FOR NEW QUESTION

OK, for this you would do pretty much the same thing. It might be a bit tricky in interface builder though as I'm never sure which is item1 and item2 in the constraint when done through IB.

You could do it very easily though by adding one line of code...

[theSuperview addConstraint:[NSLayoutConstraint constraintWithItem:labelA
                                                         attribute:NSLayoutAttributeHeight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:labelB
                                                         attribute:NSLayoutAttributeHeight
                                                        multiplier:1.0
                                                          constant:0.0]];

This is exactly what the interface builder constraint does but I'm not sure if you can tell which way around the item1 and item2 are.

This is your "unidirectional" equal heights attribute though.

EDIT 2

There may or may not be an update some time in the future that might let you see item1 and item2 in interface builder.

Upvotes: 1

Related Questions