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

jshapy8 picture jshapy8 · Jun 30, 2017 · Viewed 36.9k times · Source

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?

Answer

Siriss picture Siriss · Jun 30, 2017

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.