UIView with dynamic height multiple UILabel

空扰寡人 提交于 2019-11-29 01:37:23

问题


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.


回答1:


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.




回答2:


  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.


来源:https://stackoverflow.com/questions/36606205/uiview-with-dynamic-height-multiple-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!