记录collectionView的scrollToItem方法无效
需要注意的点:
1.如果是横向滑动的,scrollPosition的值设为.centeredHorizontally
2.当collectionView显示它的subView之前调用scrollToItemAtIndexPath: atScrollPosition:
Animated方法,是不会执行的。
可以将代码写在layoutSubViews中或者延时就能执行。
代码记录
Date.jx_after {
self.scorllToday()
}
func scorllToday() {
if let day = selModel?.year_month_day,let idx = dataSource.firstIndex(where: {$0.year_month_day.isEque(day: day)}){
let indexPath = IndexPath(row: idx, section: 0)
collectionView.isPagingEnabled = false
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
collectionView.isPagingEnabled = true
}
}
文章讨论了在使用UICollectionView时遇到的scrollToItem方法不工作的情况,特别是当滚动方向为横向时,应设置scrollPosition为.centeredHorizontally。问题可能出在collectionView的subView未显示前调用该方法。解决方案包括在layoutSubviews中或通过延迟执行来确保滚动到指定item。
1817





