UICollectionView滚动到与水平方向不兼容的项目 [英] UICollectionView scroll to item not working with horizontal direction
问题描述
我在启用了页面调度的 UIViewController
中有一个 UICollectionView
.出于某些奇怪的原因,当 collectionview
的方向为 vertical
时, collectionView.scrollToItem
可以工作,但是当方向为 horizontal 时,该方法无效代码>.这是我做错了还是应该发生?
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)
当滚动方向为 horizontal
时,您需要在 at:左侧
, at:右侧
或 at :: centeredHorizontally 代码>.
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屋!