Reputation: 2051
I have two items in a vertical UIStackView
: a UILabel
and a UITableView
. When a dynamic amount of UITableViewCell
s 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
?
Upvotes: 31
Views: 67343
Reputation: 802
A bit old the question, but it might help others nevertheless.
I was looking a long time to get this right.
If you have a vertical stack view in which your subviews are placed, create an outlet of before mentioned vertical stack view and do the following in viewDidLoad()
(of the popover view controller in my case):
self.view.layoutIfNeeded()
self.preferredContentSize.height = verticalStackView.sizeThatFits(CGSizeZero).height
Unlike what @Siriss said, sizeToFit()
was not necessary in my case.
Upvotes: 0
Reputation: 1318
I had this on iOS 12, while it was working fine on iOS 13. Just set:
stackView.distribution = .fillProportionally
Fixed it :)
Upvotes: 13
Reputation: 119
you one need to fill proportionally and it will work according to your code
Upvotes: 5
Reputation: 3777
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.
Upvotes: 29
Reputation: 161
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.
Upvotes: 16