UICollectionViewFlowLayout重写targetContentOffset后旋转设备cell不居中如何解决?
问题原因
targetContentOffset(forProposedContentOffset:withScrollingVelocity:)仅会在用户手指离开屏幕、collection view 即将开始滚动减速时被系统调用,设备旋转导致的 collection view 尺寸变化(bounds 变更)不会触发这个方法。- 你当前的布局没有在 bounds 变化时触发重新调整偏移的逻辑,因此转屏后 cell 位置会偏离。
最优解决方案(封装在布局内,无需修改ViewController)
你只需要在自定义的PagingFlowLayout中新增两个方法即可:
- 重写
shouldInvalidateLayout(forBoundsChange:),在宽度变化时触发布局刷新 - 重写另一个只有单个参数的
targetContentOffset重载,这个方法会在布局失效(比如转屏)时被系统调用,用来调整最终的内容偏移量
完整修改后的布局代码:
class PagingFlowLayout: UICollectionViewFlowLayout { var layoutAttributes = [UICollectionViewLayoutAttributes]() var contentSize = CGSize.zero override var collectionViewContentSize: CGSize { return contentSize } override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { return layoutAttributes[indexPath.item] } override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { return layoutAttributes.filter { rect.intersects($0.frame) } } // 新增:bounds变化时判断是否需要刷新布局 override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { guard let oldBounds = collectionView?.bounds else { return false } // 仅当宽度变化时才刷新,避免不必要的性能消耗 return oldBounds.width != newBounds.width } // 新增:布局失效(转屏等场景)时调整偏移量,保证cell居中 override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint { let currentOffset = collectionView?.contentOffset.x ?? 0 // 找到距离当前偏移最近的cell guard let closestAttr = layoutAttributes.min(by: { abs($0.frame.minX - currentOffset) < abs($1.frame.minX - currentOffset) }) else { return proposedContentOffset } return closestAttr.frame.origin } override func prepare() { super.prepare() guard let collectionView = collectionView else { return } let cellWidth = collectionView.bounds.width let cellHeight = collectionView.bounds.height var layoutAttributes = [UICollectionViewLayoutAttributes]() var currentCellOrigin = CGFloat(0) for index in 0..<3 { let attributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: index, section: 0)) attributes.frame = CGRect(x: currentCellOrigin, y: 0, width: cellWidth, height: cellHeight) layoutAttributes.append(attributes) currentCellOrigin += cellWidth } self.contentSize = CGSize(width: currentCellOrigin, height: cellHeight) self.layoutAttributes = layoutAttributes } // 原有的用户滑动松手时的居中逻辑 override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { let contentOffset = collectionView?.contentOffset.x ?? 0 let closestPoint = layoutAttributes.min { abs($0.frame.origin.x - contentOffset) < abs($1.frame.origin.x - contentOffset) } return closestPoint?.frame.origin ?? proposedContentOffset } }
其他说明
- 你看到的控制台
UICollectionViewRecursion相关日志是iOS 15+系统内部的调试日志,属于系统级输出,不影响应用功能,开发者无需处理。 - 如果你不想在布局内处理,也可以在ViewController中重写
viewWillTransition(to:with:)方法,在转屏动画结束后手动计算最近的cell偏移量,调用setContentOffset(_:animated:)调整,但这种方式不符合封装原则,更推荐上述布局内处理的方案。
内容的提问来源于stack exchange,提问作者aheze
相关产品推荐
相关产品推荐

