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)
    }

      

  • 相关阅读:
    Entropy
    MonkeyEatsPeach
    python中使用可选参数
    java中二元数组的构建
    静态语言和动态语言
    开胃菜
    python 工具箱
    python处理多层嵌套列表
    小球落体
    LoadRunner:Error 27796
  • 原文地址:https://www.cnblogs.com/lyjpost/p/13919590.html
Copyright © 2011-2022 走看看