简体   繁体   English

iOS标签和视图约束高度

[英]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. 我对约束确实感到困惑,因为iOS,我读过很多文章,但是在尝试根据UILabel内容高度设置UIView的 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. 在一种情况下, UIView的高度不会更改为0,而在其他情况下, UILabel的高度没有任何意义。

So. 所以。 I've got: 我有:

  1. UIView with height constraint, and descendants constraints (below) 具有高度限制和后代限制的UIView (如下)
  2. UIImageView with height=32, width=32, top=10, left=10, bottom>=10 高度= 32,宽度= 32,顶部= 10,左侧= 10,底部> = 10的 UIImageView
  3. UILabel with left to UIImageView = 10, top=10, right=10, bottom=10 UILabel左侧为UIImageView = 10,顶部= 10,右侧= 10,底部= 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 . 如果我要在UILabel中放置任何文本,我想根据UILabel内容大小的高度来动态调整UIView的高度。
  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). 如果UILabel中没有文字,我想将UIView的高度设置为0(完全隐藏UIView )。

UIImageView - is just the icon. UIImageView-只是图标。 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. 如果没有文本,则必须缩小为0,如果有文本,则必须具有高度32,顶部约束10,底部约束大于或等于10。

UPD: 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) 将四个垂直边距优先级设置为小于1000(假设为999)
  • Set the image height priority to 999 too. 将图像高度优先级也设置为999。
  • Set lable vertical hugging priority to 1000 将标签垂直拥抱优先级设置为1000
  • Set image height less than or equal label height with 1000 priority 设置图像高度小于或等于标签高度,优先级为1000
  • Set label height less than or equal container view height with multiplier 1: 10(or any big number) with priority 1000 使用优先级1000乘以1:10(或任何大数)将标签高度设置为小于或等于容器视图高度

Like this 像这样

在此处输入图片说明

The View is this 视图是这个

在此处输入图片说明

and this 和这个

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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