iOS/Swift:当 UIStackView 中的一项变大时,动态调整 UIStackView 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44837759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS/Swift: Dynamically size UIStackView when one of its items gets larger
提问by jshapy8
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.
我有两个垂直项目UIStackView
: aUILabel
和 a UITableView
。当动态数量的UITableViewCell
s 添加到UITableView
运行时,UIStackView
不会变大。
Is there a general way to increase the size of the UIStackView
?
有没有一种通用的方法来增加 的大小UIStackView
?
采纳答案by Siriss
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.
您应该在 stackView 的子视图上调用sizeToFit()
和layoutIfNeeded()
。像往常一样约束 UIStackView,并像往常一样约束子视图。
Also, you need to have it set to Fill Proportionally and it will resize to fit the new content.
此外,您需要将其设置为按比例填充,它会调整大小以适应新内容。
回答by NeroSeph
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.
确保堆栈视图上没有高度约束的底部约束。您应该只需要左、右和上约束。
回答by Muhammad Iqbal
you one need to fill proportionally and it will work according to your code
你需要按比例填充,它会根据你的代码工作
回答by Urkman
I had this on iOS 12, while it was working fine on iOS 13. Just set:
我在 iOS 12 上有这个,而它在 iOS 13 上运行良好。只需设置:
stackView.distribution = .fillProportionally
Fixed it :)
修复 :)