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?
我在UIViewController中有一个启用了分页的UICollectionView。由于一些奇怪的原因,collectionView.scrollToItem在collectionview的方向是垂直的时候工作,但是当方向是水平的时候不工作。这有什么我做错了还是这应该发生?
//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")
}
4 个解决方案
#1
18
For this part:
对于这部分:
collectionView.scrollToItem(at: i, at: .top, animated: true)
collectionView.scrollToItem(at:i,at:。top,animated:true)
When the scroll direction is horizontal
you need to use at: left
, at: right
or at: centeredHorizontally
. at: top
is for vertical
direction.
当滚动方向为水平时,您需要使用at:left,at:right或at:centeredHorizontally。 at:top表示垂直方向。
#2
5
I had trouble implementing this in a flow layout with entered paging per item. The .centeredHorizontally just wouldn't work for me so i use scroll to rect and Check there is data before scrolling:
我在流程布局中实现这一点时遇到了麻烦,每个项目都输入了分页。 .centeredHorizontally对我来说不起作用所以我使用滚动到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)
}
#3
1
Swift 3:
斯威夫特3:
This worked for me on horizontal collection view.
这在横向集合视图中对我有用。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Show 14th row as first row
self.activityCollectionView?.scrollToItem(at: IndexPath(row: 14, section: 0), at: UICollectionViewScrollPosition.right, animated: true)
}
You can choose whether the desired cell at index to be on right or left on scroll position.
您可以选择索引处的所需单元格是否在滚动位置的右侧或左侧。
UICollectionViewScrollPosition.right or UICollectionViewScrollPosition.left.
#4
0
For scrolling the item, there is no need to reloadData. Remove below line from your code and try.
要滚动项目,不需要reloadData。从代码中删除以下行并尝试。
collectionView.reloadData()
#1
18
For this part:
对于这部分:
collectionView.scrollToItem(at: i, at: .top, animated: true)
collectionView.scrollToItem(at:i,at:。top,animated:true)
When the scroll direction is horizontal
you need to use at: left
, at: right
or at: centeredHorizontally
. at: top
is for vertical
direction.
当滚动方向为水平时,您需要使用at:left,at:right或at:centeredHorizontally。 at:top表示垂直方向。
#2
5
I had trouble implementing this in a flow layout with entered paging per item. The .centeredHorizontally just wouldn't work for me so i use scroll to rect and Check there is data before scrolling:
我在流程布局中实现这一点时遇到了麻烦,每个项目都输入了分页。 .centeredHorizontally对我来说不起作用所以我使用滚动到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)
}
#3
1
Swift 3:
斯威夫特3:
This worked for me on horizontal collection view.
这在横向集合视图中对我有用。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Show 14th row as first row
self.activityCollectionView?.scrollToItem(at: IndexPath(row: 14, section: 0), at: UICollectionViewScrollPosition.right, animated: true)
}
You can choose whether the desired cell at index to be on right or left on scroll position.
您可以选择索引处的所需单元格是否在滚动位置的右侧或左侧。
UICollectionViewScrollPosition.right or UICollectionViewScrollPosition.left.
#4
0
For scrolling the item, there is no need to reloadData. Remove below line from your code and try.
要滚动项目,不需要reloadData。从代码中删除以下行并尝试。
collectionView.reloadData()