ios 具有动态高度多个 UILabel 的 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36606205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:54:34  来源:igfitidea点击:

UIView with dynamic height multiple UILabel

iosobjective-cswiftautolayoutnslayoutconstraint

提问by cheeseRoot

I'm making a custom UIView with 2 UILabel in it, with each UILabel having dynamic height. I'm unable to set constraints for dynamic height. (I'm new to Auto-Layout & iOS stuff).

我正在制作一个带有 2 个 UILabel 的自定义 UIView,每个 UILabel 都具有动态高度。我无法为动态高度设置约束。(我是自动布局和 iOS 的新手)。

Both UILabels are connected to each other from Top & Bottom, and are Horizontally aligned to superview. And when I change content of one of the UILabel, UIView and UILabel does not resize as required.

两个 UILabel 从顶部和底部相互连接,并且水平对齐到超级视图。当我更改 UILabel 之一的内容时,UIView 和 UILabel 不会根据需要调整大小。

Please suggest. Attaching screenshots for current constraints.

请建议。附加当前约束的屏幕截图。

回答by Badal Shah

I made a demofor you according to your requirement. Download it from below link,

我根据您的要求为您制作了一个演示。从下面的链接下载它,

Autoresize UIView and UILabel

自动调整 UIView 和 UILabel

Step by Step Guide :-

分步指南:-

Step 1 :-Set constrain to UIView

第 1 步:-将约束设置为 UIView

1) Leading 2) Top 3) Trailing (From mainview)

1) 领先 2) 前 3) 尾随(来自主视图)

enter image description here

在此处输入图片说明

Step 2 :-Set constrain to Label 1

第 2 步:-将约束设置为标签 1

1) Leading 2) Top 3) Trailing (From it's superview)

1) 领先 2) 前 3) 落后(从它的超级视图)

enter image description here

在此处输入图片说明

Step 3 :-Set constrain to Label 2

第 3 步:-将约束设置为标签 2

1) Leading 2) Top 3) Trailing (From it's superview)

1) 领先 2) 前 3) 落后(从它的超级视图)

enter image description here

在此处输入图片说明

Step 4 :-Most trickygive bottom to UILabel from UIView.

第 4 步:-最棘手的是从 UIView 给 UILabel 赋值。

In Xcode 10 and above - Select your UIView and Label after that from the Align Constraint section select Bottom Edges constraint.

在 Xcode 10 及更高版本中 - 选择您的 UIView 和标签,然后从“对齐约束”部分选择“底部边缘”约束。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Step 5 :-(Optional) Set constrain to UIButton

第 5 步:-(可选)将约束设置为 UIButton

1) Leading 2) Bottom 3) Trailing 4) Fixed Height (From mainview)

1) 前导 2) 底部 3) 尾随 4) 固定高度(来自主视图)

enter image description here

在此处输入图片说明

Output :-

输出 :-

enter image description here

在此处输入图片说明

Note :-Make sure you have set Number of lines =0 in Label property.

注意:-确保您在 Label 属性中设置了 Number of lines =0。

enter image description here

在此处输入图片说明

Edit1 :-If you want to increase size with BottomConstrain (>=) please check below Image.

Edit1 :-如果您想使用 BottomConstrain (>=) 增加尺寸,请查看下面的图片。

enter image description here

在此处输入图片说明

I hope this info enough to understand Autoresize UIView according to UILabel's height and Autoresize UILabel According to text.

我希望这些信息足以理解根据 UILabel 的高度自动调整 UIView 大小和根据文本自动调整 UILabel 大小。

回答by Kashif

  1. Set the Lines property of both labels to 0 in interface builder.
  2. Make sure there is no constraint for height of any of the labels or the uiview.
  3. Make sure there are no constraints on uiview which force it to a certain size, for example, if you have a constraint on uiview for certain distance from top of superview and another constraint of certain distance from bottom of superview, it will not work.
  4. Try changing the text of labels in code and height should automatically adjust.
  1. 在界面构建器中将两个标签的 Lines 属性设置为 0。
  2. 确保任何标签或 uiview 的高度都没有限制。
  3. 确保 uiview 上没有强制其为特定大小的约束,例如,如果您对 uiview 有距离超级视图顶部一定距离的约束和距离超级视图底部一定距离的另一个约束,则它将不起作用。
  4. 尝试更改代码中标签的文本,高度应自动调整。