Naresh
Naresh

Reputation: 17912

Update UICollectionView height based on content

UICollectionView height not updating properly based on content. How to update height

My code :

collectionViewArray.append(trimmedStrig!)
        DispatchQueue.main.async {
            if self.collectionViewArray.count == 1 {
                self.collectionViewHeight.constant = self.collectionView.contentSize.height + 50
                print(self.collectionViewHeight.constant)
            } else if self.collectionViewArray.count > 1 {
                self.collectionViewHeight.constant = self.collectionView.contentSize.height
                print(self.collectionViewHeight.constant)
            }
            self.collectionView.reloadData()
        }

//Fix collection view cell height and width
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: 100, height: 50)
}

When create 1st cell it's not displaying that cell, that's why I added 50 and when cell entered into second row again it's not updating height, but when 2nd row 2nd cell created in that Time height updated. Why I don't know?

Upvotes: 0

Views: 1523

Answers (1)

ishwar lal janwa
ishwar lal janwa

Reputation: 524

Try this one its may be helpful for you

self.collView.reloadData()
self.collView.performBatchUpdates({}) { (complition) in
            self.cnsHeightCollView.constant = self.collView.contentSize.height
        }

Upvotes: 1

Related Questions