ios UICollectionView - 滚动到第一个单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32262566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView - Scrolling to first cell
提问by Alessandro Garcez
I'm making use of UICollectionView and I need to scroll to first cell after click in a button.
我正在使用 UICollectionView 并且我需要在单击按钮后滚动到第一个单元格。
The button is located in a (big) header section in my collection view... when clicking int it, I need scroll to my first cell located bellow of this button.
该按钮位于我的收藏视图中的(大)标题部分...单击 int 时,我需要滚动到位于此按钮下方的第一个单元格。
I created a action of this button, but I don't know how to scroll to first UICollectionViewCell.
我创建了此按钮的操作,但我不知道如何滚动到第一个 UICollectionViewCell。
Can someone help me?
有人能帮我吗?
回答by Jacopo Penzo
Use this delegate method, setting the indexPath at the first position:
使用这个委托方法,在第一个位置设置 indexPath:
Swift
迅速
self.collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0),
atScrollPosition: .Top,
animated: true)
Swift 3
斯威夫特 3
self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0),
at: .top,
animated: true)
Objective-C
目标-C
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
atScrollPosition:UICollectionViewScrollPositionTop
animated:YES];
Change UICollectionViewScrollPositionTop
if you want to scroll and stop at a different position
更改UICollectionViewScrollPositionTop
是否要滚动并停在不同的位置
回答by Alessandro Garcez
It might be possible to use the UIScrollView related property
可能可以使用 UIScrollView 相关属性
collectionView.contentOffset.x = 0
回答by Celil Bozkurt
You can use this for Objective - C
您可以将其用于 Objective-C
[self.restaurantCollectionView setContentOffset:CGPointZero animated:YES];
回答by Celil Bozkurt
The following solution works fine for me:
以下解决方案对我来说很好用:
UIView.animate(withDuration: 0.5, animations: {
self.collectionView?.contentOffset.x = 0
})
It scrolls to the first item and the contentInset can be seen as well.
它滚动到第一项,也可以看到 contentInset。
回答by JP Aquino
scrollView.setContentOffset(CGPoint(x: 0.0, y: 0.0), animated: true)
回答by zekel
I found to work reliably on all devices and correctly handle iPhone X-style safe areas.
我发现在所有设备上都能可靠地工作,并能正确处理 iPhone X 风格的安全区域。
let top = CGPoint(x: collectionView.contentOffset.x,
y: collectionView.adjustedContentInset.top)
collectionView.setContentOffset(top, animated: false)
(This scrolls vertically to the top but you could easily make it scroll to the top-left if you wanted.)
(这会垂直滚动到顶部,但如果需要,您可以轻松地将其滚动到左上角。)
回答by Kavita
Default CollectionView Delgeate...
默认 CollectionView 委托...
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
回答by Sami Yousif
if let indexPath = self.collectionView?.indexPathForItem(at: CGPoint(x: 0, y: 0)) {
self.collectionView?.scrollToItem(at: indexPath, at: .top, animated: false)
}