主要有三点:
1.设置collectionView的layout.estimatedItemSize
2.在自定义cell中重写
override func preferredLayoutAttributesFitting (_layoutAttributes: UICollectionViewLayoutAttributes ) - >UICollectionViewLayoutAttributes {
self.setNeedsLayout()
self.layoutIfNeeded()
letsize
=self.contentView.systemLayoutSizeFitting(layoutAttributes.size)
varnewFrame = layoutAttributes.frame
newFrame.size.height= size.height
layoutAttributes.frame= newFrame
returnlayoutAttributes
}
3.layout的sectionInsert不能设置
这样操作下来,如果collectionView为横向滑动,cell会贴着上下两边
如果为纵向滑动,cell会贴着左右两边
但是这个问题怎么解决,我还没有找到办法