Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 633
jshapy8 Avatar asked Jun 30 '17 02:06

jshapy8


4 Answers

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.

like image 187
Siriss Avatar answered Nov 18 '22 19:11

Siriss


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.

like image 28
NeroSeph Avatar answered Nov 18 '22 20:11

NeroSeph


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

stackView.distribution = .fillProportionally

Fixed it :)

like image 11
Urkman Avatar answered Nov 18 '22 18:11

Urkman


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

like image 5
Muhammad Iqbal Avatar answered Nov 18 '22 20:11

Muhammad Iqbal