如何根据 ios 中的集合视图行数使滚动视图增长?

how to make scrollview grow based on collection view number of rows in ios?

我有一个滚动视图,它有一个图片、文本、按钮、标签,然后是一个行数本质上是动态的 collectionView。它像 collectionview 的高度会增加,因此 scrollview 应该假定 collectionView 高度以及其他元素的高度以获得平滑的滚动。如何在 ios 中使用自动布局来实现这一点?

所以我的疑问是如何根据不断增长的 colelctionview 或 tableview 高度制作动态滚动视图高度。

您可以增加 CollectionView 高度约束的高度,将高度包含连接到您的 ViewController 而不是 .

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

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)
}

这些方法对我不起作用。试试,

self.collectionView.reloadData()
DispatchQueue.main.async {
    self.heightCollectionViewConstraint.constant = self.collectionViews.collectionViewLayout.collectionViewContentSize.height
}