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?
Asked
Active
Viewed 282 times
1
-
can you give some rough diagram? – Yuyutsu Jun 17 '15 at 08:45
2 Answers
1
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:
Make sure your labels has horizontal compression resistance set to required (1000) to prevent labels from being down-resized.

Sega-Zero
- 3,034
- 2
- 22
- 46
0
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