Aarkan
Aarkan

Reputation: 4109

Choosing maximum of two widths for auto layout

I have a UIImageView and two two UILabel as its child. Now using auto layouts I want that UIImageView's width should be equal to the maximum of these two labels so that both of these are visible completely. Is there a way to define this constraint?

Upvotes: 1

Views: 288

Answers (2)

Sega-Zero
Sega-Zero

Reputation: 3017

Instead of using same width constraint (which is fine too if you specify <= constraints, but needs some extra work after) you may use leading and trailing constraints. If your imageview will have a >= leading and <= trailing constraints for both of the labels + your labels will be in their intrinsic size, then imageview will have the maximum width. This can be illustrated with the picture below: example of IB configure
Make sure your labels has horizontal compression resistance set to required (1000) to prevent labels from being down-resized.

Upvotes: 1

iKatalan
iKatalan

Reputation: 143

You can define your constraints in the code itself and add a constraint after you know the size of the wider UILabel

Take a look at this answer on how to add constraints programmatically

https://stackoverflow.com/a/13055845

Upvotes: 0

Related Questions