[英]how to make scrollview grow based on collection view number of rows in ios?
i have a scrollview which has a picture, text, button, label and then a collectionView whose number of rows is dynamic in nature.我有一个滚动视图,它有一个图片、文本、按钮、label,然后是一个行数本质上是动态的 collectionView。 Its like collectionview will grow in height and hence the scrollview should assume collectionView height as well as other elements height to get a smooth scroll.它像 collectionview 的高度会增加,因此 scrollview 应该假定 collectionView 高度以及其他元素的高度以获得平滑的滚动。 How to achieve this using auto Layout in ios?如何在 ios 中使用自动布局来实现这一点?
So my doubt is how to make a dynamic scrollview height based on a growing colelctionview or tableview height.所以我的疑问是如何根据不断增长的 colelctionview 或 tableview 高度制作动态滚动视图高度。
You can increase the height of CollectionView
Height Constraint , connect Height contains to your ViewController
and than .您可以增加CollectionView
Height Constraint 的高度,将 Height contains 连接到您的ViewController
和 than 。
how to connect constraint to outlet ?如何将约束连接到插座?
let height = self.collectionView.contentSize.height;
self.heightConstraints = height;
self.scrollView.contentSize = CGSize.init(widht:self.scrollView.frame.size.width,height:height)
FOr Swift 4.2对于斯威夫特 4.2
override func viewDidLayoutSubviews() {
let heightFilterCOll = self.filterCollectionView.contentSize.height
self.filterCOllectionViewCOnstraintHeight.constant = heightFilterCOll
self.ScrollView.contentSize = CGSize(width:self.ScrollView.frame.size.width,height:heightFilterCOll + 77)
}
These methods didn't work for me.这些方法对我不起作用。 Try,尝试,
self.collectionView.reloadData()
DispatchQueue.main.async {
self.heightCollectionViewConstraint.constant = self.collectionViews.collectionViewLayout.collectionViewContentSize.height
}
@ Wimukthi Rajapaksha @Wimukthi Rajapaksha
try his solutions its working perfectly.试试他的解决方案,它工作得很好。 Just keep in mind to specify a height of collection in scroll view.请记住在滚动视图中指定集合的高度。 create an outlet of the height constant.创建一个高度常数的出口。 Reload table and assign the dynamic height.重新加载表格并分配动态高度。 Thanks.谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.