美文网首页
collectionView的scrollToItem:移位问题

collectionView的scrollToItem:移位问题

作者: 确认过眼神啊 | 来源:发表于2022-08-23 11:08 被阅读0次

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

相关文章

网友评论

      本文标题:collectionView的scrollToItem:移位问题

      本文链接:https://www.haomeiwen.com/subject/dzxggrtx.html