collectionView的scrollToItem:移位问题

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容