4

Inside a UItableview Cell I have following structure.

enter image description here

It has two Stackviews Left and Right
1) Left Stackview contains 3 views. Each view containing 1 label (Label1,Label2,Label3)
2) Right Stackview contains 2 views. Each view containing 1 label (Label5,Label6)

Constraints for Label1,Label2 and Label3 are Top,Left,Right and Bottom with respect to it's parent views.

Now when I add text to Label1 , Label2 and Label3 at run time it gives dynamic height but leaves space in top and bottom, for those label having less text. Below is the output.

enter image description here

I want to reduce the top and bottom spaces for all the three labels i.e Label1,Label2,Label3.

Any help will be appreciated.

user7420795
  • 199
  • 1
  • 15

3 Answers3

5

This may help you

I think You have use distribution of Stack view as fill equally so it gives equal space to each one. I think for this you have to use fill proportionally. this will adjust height accordingly

Choose fill proportionally from here

if this not working then choose fill proportionally and give height to each label it will adjust.

Check how to give height to labels . enter image description here

after giving each label height inside stack view like this . your it will adjust itself according to data.Like this

enter image description here

Amit gupta
  • 553
  • 3
  • 10
  • If I apply fill proportionally then nothing is visible in Table view. – user7420795 Aug 17 '18 at 10:16
  • Check Constrains is it correct as this is working fine on my end.I think you forgot to git height to each label .Give height to each label and make it greater then or equals to then it will work fine – Amit gupta Aug 20 '18 at 11:57
  • thanks for the reply @Amit. I have given the constraint as mentioned in my question. i.e (left, Right ,Top and Bottom) . But still it is not working. – user7420795 Aug 23 '18 at 12:25
  • 1
    No, you are not getting my point .I mean give height to each label greater then or equal .I will update my above answer with two new screenshot please check. – Amit gupta Aug 23 '18 at 12:43
2

For dynamic height use self sizing cells in tableview. Set rowHeight and estimatedHeight properties for tableView

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100

Set the stackview distribution property to .fillproportionally

as per the UIStackView documentation of .fillproportionally property

A layout where the stack view resizes its arranged views so that they fill the available space along the stack view’s axis. Views are resized proportionally based on their intrinsic content size along the stack view’s axis.

in code:

stackView.distribution = fillproportionally

or in storyboard

enter image description here

Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
2

Please check the attached screenshot, there are showing fully step by step guide line. How to set UILabel dynamically height inside vertical stackview with multiple UILabel's.

If you have any queries please comment.

Check the attached screenshot