UICollectionView滚动到与水平方向不兼容的项目 [英] UICollectionView scroll to item not working with horizontal direction

查看:75
本文介绍了UICollectionView滚动到与水平方向不兼容的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在启用了页面调度的 UIViewController 中有一个 UICollectionView .出于某些奇怪的原因,当 collectionview 的方向为 vertical 时, collectionView.scrollToItem 可以工作,但是当方向为 horizo​​ntal 时,该方法无效代码>.这是我做错了还是应该发生?

I have a UICollectionView within a UIViewController with paging enabled. For some strange reason collectionView.scrollToItem works when the direction of the collectionview is vertical but doesn't when direction is horizontal. Is this there something I'm doing wrong or is this supposed to happen?

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

推荐答案

为此部分:

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

当滚动方向为 horizo​​ntal 时,您需要在 at:左侧 at:右侧 at :: centeredHorizo​​ntally . at:top 是用于 vertical 方向.

When the scroll direction is horizontal you need to use at: left, at: right or at: centeredHorizontally. at: top is for vertical direction.

这篇关于UICollectionView滚动到与水平方向不兼容的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆