在我的项目中,我正在尝试为UICollectionView
实现清晰的分页功能。但是,当我向UICollectionView中添加单元格时,collectionView奇怪地向下跳了几个像素。
我确实使用UICollectionViewCell
的UICollectionViewDelegateFlowLayout
,sizeForItem
的UICollectionViewCell
和preferredLayoutAttributesFitting
将AutoLayout
设置为固定宽度和动态高度。
我已经尝试过prefferedLayoutAttributesFitting的不同实现,并且在观察collectionView的偏移量值后,我可以知道偏移量正方面变化,并且对行为不负责。
当我使用RxSwift
和RxCocoa
时,我不是自己调用reloadData()。
某些代码:
UICollectionView - setup code
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 16
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
layout.sectionInsetReference = .fromContentInset
layout.sectionInset = UIEdgeInsets.init(top: 0, left: 16, bottom: 0, right: 16)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(SomeCell.self, forCellWithReuseIdentifier: "someCell")
collectionView.backgroundColor = .clear
collectionView.scrollIndicatorInsets = UIEdgeInsets.init(top: 76, left: 0, bottom: 0, right: 0)
collectionView.contentInset = UIEdgeInsets.init(top: 76 + 16, left: 0, bottom: 16, right: 0)
collectionView.refreshControl = UIRefreshControl()
return collectionView
}()
UICollectionViewDelegateFlowLayout - sizeForItem
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let sectionInset = (collectionViewLayout as! UICollectionViewFlowLayout).sectionInset
let referenceHeight: CGFloat = 350
let referenceWidth = collectionView.safeAreaLayoutGuide.layoutFrame.width
- sectionInset.left
- sectionInset.right
- collectionView.contentInset.left
- collectionView.contentInset.right
return CGSize(width: referenceWidth, height: referenceHeight)
}
UICollectionViewCell - preferredLayoutAttributes
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
titleLabel.preferredMaxLayoutWidth = layoutAttributes.size.width - contentView.layoutMargins.left - contentView.layoutMargins.left
someLabel.preferredMaxLayoutWidth = layoutAttributes.size.width - contentView.layoutMargins.left - contentView.layoutMargins.left
layoutAttributes.bounds.size.height = systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
return layoutAttributes
}
如果您想查看更多特定的代码,请随时询问!
根据我的经验,通过集合视图进行分页时,如果单元格不是屏幕的完整宽度或高度(分别取决于水平或垂直分页),就会出现问题。在视频中,由于您的参考高度仅为350,因此您的单元格可能无法完全适合屏幕尺寸。如果是这种情况,则需要考虑单元格的正确插入和行间距,以使分页与您的页面对齐内容。
这些是我发现对解决此问题至关重要的方法(这些值假定单元沿分页的方向占据了整个屏幕的大小):
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0)
}
如果您的单元格没有填满屏幕,请相应地调整这些值,以使您的单元格不会出现在屏幕上/屏幕中间的位置。
如果这没有帮助,那么我认为这可能与您添加单元格的方式有关,但是根据您发布的代码,我看不到这是怎么回事。希望这会有所帮助!