swift 5.0时,滚动UICollectionView到指定的indexPath时,出现上移动的情况,莫名其妙,下面是修复后前后的效果对比,暂时还不知道是啥愿意,先记录一下解决办法.
@objc private func itemClick(item: UIBarButtonItem) {
print(item.tag)
// 1.获取点击的item的tag
let tag = item.tag
// 2.根据tag获取当前组
let indexPath = IndexPath(item: 0, section: tag)
// 3.滚动到对应的位置
// 避免错位,需要添加isPagingEnabled = false 然后再设置回来isPagingEnabled = true
collectionView.isPagingEnabled = false
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
collectionView.isPagingEnabled = true
}
蒋飞 2022-08-08 11.01.18.gif
蒋飞 2022-08-08 11.01.20.gif