Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding UIView as UILabel inside grows

I have a UIView which is a superview of UIImageView and UILabel. UILabel can have different size depending on the amount of text in it. So, I need the UIView to grow and shrink together with the UILabel but can not make it working.

The screenshot of how it looks is below:

enter image description here

As you can see the spacing above and below of the label are too big. In the .xib I set up top and bottom constraints on the UILabel to be just 10 but apparently these are somehow violated. UIView has top constraint to the frame above with the image as >=10, so that it could grow from 10.

You can see .xib below:

enter image description here

I tried to increase Content Hugging Priority of the UIView but it does not help. UILabel has just 4 constraints: top, bottom, left, right relative to its superview UIView. I have also tried to change Content Hugging Priority of UILabel relative to UIView but no success. The UIImageView is the actual border of the UIView that is shown in the picture.

like image 228
Nikita Vlasenko Avatar asked Jan 26 '16 20:01

Nikita Vlasenko


2 Answers

Think you're missing:

View:

  • Constraint: Height >= 20

Label:

  • Lines: 0
  • Constraint: Top and bottom margins to the (super) View
  • Constraint: Height >= 10

Screenshot of a working example:

enter image description here

Tip: use the preview

like image 176
Sjoerd Perfors Avatar answered Nov 06 '22 19:11

Sjoerd Perfors


So, in my case the solution was to set constraints of UILabel relative to the UIImageView and then set the Content Compression Resistance Priority to 250. Then everything started to work. Thank you matt! I opened your github project and found that Content Compression Resistance Priority of UIImageView should be 250 - this was crucial.

like image 35
Nikita Vlasenko Avatar answered Nov 06 '22 21:11

Nikita Vlasenko