我正在制作一个包含 2 个 UILabel 的自定义 UIView,每个 UILabel 都具有动态高度.我无法为动态高度设置约束.(我是 Auto-Layout 和 iOS 方面的新手).
两个 UILabel 从 Top &底部,并与超级视图水平对齐.当我更改 UILabel 之一的内容时,UIView 和 UILabel 不会根据需要调整大小.
请提出建议.附上当前约束的屏幕截图.
我根据你的要求为你做了一个demo.从下面的链接下载它,
第 2 步:- 将约束设置为标签 1
1) 领先 2) 前 3) 尾随(来自它的超级视图)
第 3 步:- 将约束设置为标签 2
1) 领先 2) 前 3) 尾随(来自它的超级视图)
第 4 步:- 最棘手的从 UIView 给 UILabel 底部.
在 Xcode 10 及更高版本中 - 选择您的 UIView 和标签,然后从对齐约束部分选择底部边缘约束.
第 5 步:-(可选)将约束设置为 UIButton
1) 领先 2) 底部 3) 尾随 4) 固定高度(从主视图)
输出:-
注意:-请确保您已在 Label 属性中设置 Number of lines =0.
Edit1 :- 如果您想使用 BottomConstrain (>=) 增加尺寸,请查看下面的图片.
我希望这些信息足以理解 Autoresize UIView 根据 UILabel 的高度和 Autoresize UILabel 根据文本.
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).
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.
Please suggest. Attaching screenshots for current constraints.
I made a demo for you according to your requirement. Download it from below link,
Autoresize UIView and UILabel
Step by Step Guide :-
Step 1 :- Set constrain to UIView
1) Leading 2) Top 3) Trailing (From mainview)
Step 2 :- Set constrain to Label 1
1) Leading 2) Top 3) Trailing (From it's superview)
Step 3 :- Set constrain to Label 2
1) Leading 2) Top 3) Trailing (From it's superview)
Step 4 :- Most tricky give bottom to UILabel from UIView.
In Xcode 10 and above - Select your UIView and Label after that from the Align Constraint section select Bottom Edges constraint.
Step 5 :- (Optional) Set constrain to UIButton
1) Leading 2) Bottom 3) Trailing 4) Fixed Height (From mainview)
Output :-
Note :- Make sure you have set Number of lines =0 in Label property.
Edit1 :- If you want to increase size with BottomConstrain (>=) please check below Image.
I hope this info enough to understand Autoresize UIView according to UILabel's height and Autoresize UILabel According to text.
这篇关于具有动态高度多个UILabel的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!