0

我知道这已经在很多线程中被问到了,但是没有一个解决方案对我有用。问题场景是:我在表格视图单元格中有一个集合视图。集合视图的约束如下:Collectionview 顶部、前导、尾随和底部约束到 superview 和 heightconstraint(优先级 999)。将集合视图的高度更改为:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GalleryCollectionViewCell.identifier, for: indexPath) as? GalleryCollectionViewCell {
        collectionViewHeightConstraint.constant = collectionView.contentSize.height
        self.layoutIfNeeded()
        return cell
    }
    return UICollectionViewCell()
}

单击按钮,重新加载 tableview,当打印 collectionview 高度约束值时,它实现了,但 tableview 单元格没有得到准确的内容大小。但是当我滚动时,tableview 单元格会得到确​​切的大小。

PS:也尝试使用:

collectionViewHeightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
4

2 回答 2

0

通过设置收集流布局试试这个: -

var flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: 200, height: collectionView.contentSize.height)
flowLayout.minimumInteritemSpacing = 0
flowLayout.scrollDirection = .vertical
CollectionVW.collectionViewLayout = flowLayout
于 2018-04-20T06:44:34.920 回答
0

在您的collectionviewCell中,您需要使用 ContentView 并设置常量高度

    self.contentView.translatesAutoresizingMaskIntoConstraints = false
    Heightconstant.constant = 200
于 2018-04-20T04:55:30.737 回答