我想显示宽度固定且高度波动的UICollectionViewCell。
由于分页,当元素数量增加时,就会出现滚动位置移动的问题。
我尝试了各种方法,但是并没有改变。
这是代码。 (仅重要要点)
final class OwnerTimelineViewController: UIViewController {
@IBOutlet private weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.minimumLineSpacing = 16
flowLayout.sectionInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
flowLayout.estimatedItemSize = CGSize(width: collectionView.bounds.width - 32,
height: 300)
}
collectionView.dataSource = self
collectionView.registerNib(type: TimelineImagesCollectionViewCell.self)
collectionView.registerNib(type: TimelineMessageCollectionViewCell.self)
}
}
final class TimelineImagesCollectionViewCell: UICollectionViewCell {
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
layoutIfNeeded()
let size = contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
var frame = layoutAttributes.frame
frame.size.height = ceil(size.height)
layoutAttributes.frame = frame
return layoutAttributes
}
}
如何使其平滑滚动?