Now, here’s a scenario where I have a uicollectionView nested in my tabelViewCell, but the height of the uicollectionView is uncertain, and I use automatic layout to calculate the height of the UITabelViewCell, The height inside the UITabelViewCell is supported by the UICcollectionView. But found that every time it just came in calculating error, later found collectionViewContentSize this elder brother the size is not accurate. The width is smaller than I calculated, so the height is wrong. Solutions:

self.collectionView.collectionViewLayout.invalidateLayout()

self.collectionView.reloadData()

self.collectionView.setNeedsLayout()

self.collectionView.layoutIfNeeded()

let h = self.collectionView.collectionViewLayout.collectionViewContentSize.height

self.h = h

self.collectionView.snp.updateConstraints { make in

make.height.equalTo(h)

}

or

override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {

let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)

self.collectionView.layoutIfNeeded()

let height = self.collectionView.collectionViewLayout.collectionViewContentSize.height

return CGSize(width: size.width,height: size.height + height)

        }