1

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?

Aarkan
  • 3,811
  • 6
  • 40
  • 54

2 Answers2

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: example of IB configure
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

https://stackoverflow.com/a/13055845

Community
  • 1
  • 1
iKatalan
  • 143
  • 7