iOS/Swift: Dynamically size UIStackView when one of its items gets larger

47,403

Solution 1

You should call sizeToFit() and layoutIfNeeded() on the subviews of the stackView. Constrain the UIStackView as you normally would, and constrain the subviews as you normally would.

Also, you need to have it set to Fill Proportionally and it will resize to fit the new content.

Solution 2

Make sure you don't have a bottom constraint of a height constraint on the stackview. You should only need left, right and top constraint.

Solution 3

I had this on iOS 12, while it was working fine on iOS 13. Just set:

stackView.distribution = .fillProportionally

Fixed it :)

Solution 4

you one need to fill proportionally and it will work according to your code

Share:
47,403
jshapy8
Author by

jshapy8

Using software to solve problems. Currently doing it with iOS apps written in Swift.

Updated on July 09, 2022

Comments

  • jshapy8
    jshapy8 almost 2 years

    I have two items in a vertical UIStackView: a UILabel and a UITableView. When a dynamic amount of UITableViewCells get added to the UITableView at runtime, the UIStackView does not get larger.

    Is there a general way to increase the size of the UIStackView?