UICollectionView滚动到不使用水平方向的项目

kye*_*kye 20 ios uicollectionview swift

我有一个启用分页的UICollectionView内部UIViewController.由于一些奇怪的原因,collectionView.scrollToItem当方向collectionview是,vertical但不是方向时工作horizontal.这有什么我做错了或者这应该发生吗?

  //Test scrollToItem
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let i = IndexPath(item: 3, section: 0)
    collectionView.reloadData()
      collectionView.scrollToItem(at: i, at: .top, animated: true)
      print("Selected")
  }
Run Code Online (Sandbox Code Playgroud)

Lun*_*irl 47

对于 iOS 14

显然有一个新的错误UICollectionView导致scrollToItem在启用分页时无法工作。解决方法是在调用之前禁用分页scrollToItem,然后在调用之后重新启用它:

collectionView.isPagingEnabled = false
collectionView.scrollToItem(
    at: IndexPath(item: value, section: 0),
    at: .centeredHorizontally,
    animated: true
)
collectionView.isPagingEnabled = true
Run Code Online (Sandbox Code Playgroud)

来源:https : //developer.apple.com/forums/thread/663156

  • 谢谢,这是我正在寻找的解决方案。奇怪,希望他们尽快修复。造成了一些不必要的不​​良用户体验 (3认同)

kl.*_*oon 31

对于这部分:

collectionView.scrollToItem(at: i, at: .top, animated: true)

horizontal您需要使用滚动方向时at: left,at: rightat: centeredHorizontally.at: top是为了vertical方向.


Ked*_*kar 10

斯威夫特 5.1,Xcode 11.4

collectionView.scrollToItem(at: IndexPath(item: pageNumber , section: 0), at: .centeredHorizontally, animated: true)
self.collectionView.setNeedsLayout() // **Without this effect wont be visible**
Run Code Online (Sandbox Code Playgroud)

  • 你不应该直接调用 `layoutSubviews()` 方法。如果要强制更新布局,请在下一次绘图更新之前调用 `setNeedsLayout()` 方法。如果您想立即更新视图的布局,请调用 `layoutIfNeeded()` 方法。https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews (6认同)

Pip*_*ppo 9

我在流程布局中实现此功能时遇到了问题,每个项目都输入了分页..centeredHorizo​​ntally对我来说不起作用所以我使用scroll to rect并在滚动之前检查数据:

    if self.collectionView?.dataSource?.collectionView(self.collectionView!, cellForItemAt: IndexPath(row: 0, section: 0)) != nil {
        let rect = self.collectionView.layoutAttributesForItem(at: IndexPath(item: data[index], section: 0))?.frame
        self.collectionView.scrollRectToVisible(rect!, animated: false)
    }
Run Code Online (Sandbox Code Playgroud)

  • 这适用于 swift 5.3 - IOS 14 - xCode 12。通常的scrollToItem 对我来说在 IOS 14 中不再工作,它在 ios 13.7 和 swift 5.2 中工作(一周前)。多谢 ! (2认同)

小智 9

我尝试了很多事情但都失败了。这节省了我的时间。

     func scrollToIndex(index:Int) {
       let rect = self.collectionView.layoutAttributesForItem(at: IndexPath(row: index, section: 0))?.frame
       self.collectionView.scrollRectToVisible(rect!, animated: true)
     }
Run Code Online (Sandbox Code Playgroud)

参考:https ://stackoverflow.com/a/41413575/9047324


ali*_*ner 5

对我来说,我必须在主线程上滚动集合视图,例如:

    DispatchQueue.main.async {
        self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
    }
Run Code Online (Sandbox Code Playgroud)


小智 5

将项目添加到 collectionView 和 后reloadData()scrollToItem由于重新加载数据尚未完成而无法正常工作。因此,我像这样添加了 performBatchUpdates :

self.dataSource.append("Test")
self.collectionView.performBatchUpdates ({
    self.collectionView.reloadData()
}, completion: { _ in 
    self.collectionView.scrollToItem(at: IndexPath(item: 3, section: 0),
     at: .centeredHorizontally,
     animated: false)
})
Run Code Online (Sandbox Code Playgroud)

我知道这不是关于这个问题,但它会对这个标题有所帮助。


bar*_*che 5

我有这个问题:当点击按钮(水平集合滚动到下一个项目)时,它总是返回到第一个带有 UI 错误的项目。

原因在这个参数中: myCollection.isPagingEnabled = true

解决方案:只需在滚动到下一项之前禁用分页,并在滚动后启用它。


小智 5

scrollToItem如果您之前打电话,这将不起作用viewDidLayoutSubviews。使用scrollToIteminviewDidLoad将不起作用。所以viewDidLayoutSubviews()完成后调用这个函数。