zoukankan      html  css  js  c++  java
  • scrollToItem出错

    原本我是这样写的

    // 滚动到指定位置
    DispatchQueue.main.async { [weak self] in
      let indexPath = IndexPath(row: self!.currentNumber - 1, section: 0)
      self!.collectionView.scrollToItem(at: indexPath, at: .left, animated: true)
    }

    发现问题,UICollectionView在iOS14上滚动不到指定indexPath,而在iOS14以下是正常的,莫名其妙

    解决方法,添加UICollectionView公开扩展方法,计算滚动偏移量

    public extension UICollectionView {
        func scrollTo(indexPath: IndexPath, animated: Bool = true) {
            let attributes = collectionViewLayout.layoutAttributesForItem(at: indexPath)!
            setContentOffset(attributes.frame.origin, animated: animated)
        }
    }
    

    修改后的代码

    // 初始滚动到指定位置
    DispatchQueue.main.async { [weak self] in
      let indexPath = IndexPath(row: self!.currentNumber - 1, section: 0)
      self!.collectionView.scrollTo(indexPath: indexPath)
    }

      

  • 相关阅读:
    线性回归的从零开始实现
    比赛总结
    计数学习笔记
    DP计数问题
    多项式学习笔记
    数据结构学习笔记
    子集运算学习笔记
    待学习
    ICPC2018焦作 题解
    ICPC2018焦作 H题 Can You Solve the Harder Problem?
  • 原文地址:https://www.cnblogs.com/lyjpost/p/13919590.html
Copyright © 2011-2022 走看看