0

I know this is already asked in many threads but none of the solutions are working for me. Problem scenario is: I have a collectionview in tableview cell. The constraints on collection view are as: Collectionview top, leading, trailing and bottom constraint to superview and heightconstraint (with priority 999). Changing the height of collection view as:

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

On a button click the tableview is reload and when collectionview height constraint value is printed, it comes true but the tableview cell is not getting exact content size. But when i scroll then the tableview cell gets the exact size.

PS: Also tried using :

collectionViewHeightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
cherry_4
  • 158
  • 2
  • 17

2 Answers2

0

In your collectionviewCell, you need to take ContentView and set constant Height

    self.contentView.translatesAutoresizingMaskIntoConstraints = false
    Heightconstant.constant = 200
kalpesh
  • 1,285
  • 1
  • 17
  • 30
0

try this by setting collection flow layout:-

var flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: 200, height: collectionView.contentSize.height)
flowLayout.minimumInteritemSpacing = 0
flowLayout.scrollDirection = .vertical
CollectionVW.collectionViewLayout = flowLayout
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19