How can I make my UIImage the same height as the intrinsic height of my UILabel using AutoLayout?

时间:2017-08-05 12:11:13

标签: ios xcode autolayout

I want to use a horizontal stack view to place an icon (UIImage) next to a label (UILabel). I want it so the icon is always the same height as the label's text. If the user increases the system font size, I want the icon to adjust accordingly to remain the same height.

Note the stack view with the icon and label are in a UITableViewCell.

How can I do this?

So far I have the following AutoLayout settings:

  • UITableViewCell height set to default.
  • Stack view constrained to the size of the UITableViewCell.
  • Stack view alignment and distribution set to fill.
  • UILabel content hugging priority 250 and compression resistance priority 750.
  • UIImageView content hugging priority 250 and compression resistance priority 749.
  • UIImageView aspect ratio 1:1.
  • UIImageView the same height as the UILabel.

All I get is this: enter image description here

Needless to say I have tried many different combinations but this is the best I get. I want the UIImageView (Facebook logo) to be the same height as 'Some text'.

I want this: enter image description here

1 个答案:

答案 0 :(得分:4)

随着图像的增长,您的标签会增长,但您需要相反的行为。您可以通过内容压缩阻力优先级和内容拥抱优先级来控制它。您应该将标签的垂直内容拥抱优先级设置为必需。这将禁止标签变得比其自然高度更大。将图像的垂直内容压缩阻力优先级设置为低,这可能会使图像缩小到标签高度。

相关问题