简体   繁体   中英

IOS Label and View constraints height

I am really confused about constraints is iOS, i've read a lot of articles, but get stuck, when tried to set Height constraints of UIView according to UILabel content height. I know that is the common question, but i really don't understand the solutions. I thought that the main thing in this question is the constraint priority , but i can't set them properly. In one case, the height of UIView wont change to 0, and in other UILabel height does not make any sense.

So. I've got:

  1. UIView with height constraint, and descendants constraints (below)
  2. UIImageView with height=32, width=32, top=10, left=10, bottom>=10
  3. UILabel with left to UIImageView = 10, top=10, right=10, bottom=10

And i want:

  1. If i got any text to place in the UILabel , i want to dynamic height of UIView according to height of content size of UILabel .
  2. And if there is no text to place in UILabel , i want to set the height of UIView equals to 0 (hide the UIView totally).

UIImageView - is just the icon. If there is no text, must be shrink to 0, if there is some text, must have height 32, top constraint 10, bottom constraint more or equals to 10.

UPD:

Fix the problem, by adding height constraint programmatically. (don't like this)

You can make the constraints take full control of the label,image and view heights, if you set the priorities correctelly.

  • Set the four vertical margins priorities to less than 1000 (lets say 999)
  • Set the image height priority to 999 too.
  • Set lable vertical hugging priority to 1000
  • Set image height less than or equal label height with 1000 priority
  • Set label height less than or equal container view height with multiplier 1: 10(or any big number) with priority 1000

Like this

在此处输入图片说明

The View is this

在此处输入图片说明

and this

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM