以编程方式滚动到UICollectionView中的补充​​视图 [英] Programmatically scroll to a supplementary view within UICollectionView

查看:91
本文介绍了以编程方式滚动到UICollectionView中的补充​​视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UICollectionView 来分段显示照片。每个部分都有一个补充视图作为标题,并通过以下方法提供: viewForSupplementaryElementOfKind

I am using UICollectionView to display photos in sections. Each section has a supplementary view as a header and is supplied via the method: viewForSupplementaryElementOfKind.

我的侧面有一个擦洗器,允许用户从一个区域跳到另一个区域。现在我使用 scrollToItemAtIndexPath:atScrollPosition:animated:滚动到该部分中的第一项,但我真正想要的是滚动collectionView以便该部分的标题位于屏幕的顶部,而不是第一个单元格。我没有看到一个明显的方法来做到这一点。你们有没有解决过的问题?

I have a scrubber on the side that allows the user to jump from section to section. For now I am scrolling to the first item in the section using scrollToItemAtIndexPath:atScrollPosition:animated:, but what I really want is to scroll the collectionView so that that section's header is at the top of the screen, not the first cell. I do not see an obvious method to do this with. Do any of you have a work around?

我想我可以滚动到该部分的第一项,然后通过补充高度加上之间的偏移来抵消如果它属于那个项目和标题(有一种方法可以滚动到contentView的点坐标)。但是,如果有一种更简单的方法,我想知道。

I suppose I could scroll to the first item of the section, and then offset that by the supplementary height plus the offset between the items and header if it comes down to that (there is a method for scrolling to point coordinates of the contentView). However if there is a simpler way, I'd like to know.

谢谢。

推荐答案

不能使用 scrollToItemAtIndexPath:atScrollPosition:animated 为此。

希望他们会添加一个像<$的新方法c $ c> scrollToSupplementaryElementOfKind:atIndexPath:将来,但目前,唯一的办法是直接操作contentOffset

Hopefully, they will add a new method like scrollToSupplementaryElementOfKind:atIndexPath: in the future, but for now, the only way is to manipulate the contentOffset directly.

下面的代码显示了如何使用FlowLayout将标题滚动到垂直顶部。您可以对水平滚动执行相同操作,或将此想法用于其他布局类型。

The code below shows how to scroll header to be on top vertically with FlowLayout. You can do the same for horizontal scrolling, or use this idea for other layout types.

NSIndexPath *indexPath = ... // indexPath of your header, item must be 0

CGFloat offsetY = [collectionView layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath].frame.origin.y;

CGFloat contentInsetY = self.contentInset.top;
CGFloat sectionInsetY = ((UICollectionViewFlowLayout *)collectionView.collectionViewLayout).sectionInset.top;

[collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x, offsetY - contentInsetY - sectionInsetY) animated:YES];

请注意,如果您有非零 contentInset (如在iOS 7中,当滚动视图扩展到条形下方时),您需要从 offsetY 中减去它,如图所示。同样适用于 sectionInset

Note that if you have non-zero contentInset (as in iOS 7, when scroll views expand below bars) you need to subtract it from the offsetY, as shown. Same for sectionInset.

更新:

代码假定布局处于准备好的有效状态,因为它使用它来计算偏移量。当集合视图显示其内容时,将准备布局。

The code assumes that the layout is in prepared, "valid" state because it uses it to calculate the offset. The layout is prepared when the collection view presents its content.

之前对 [_ collectionView.collectionViewLayout prepareLayout] 的调用当您需要滚动尚未显示的集合视图时,上面的代码可能有所帮助(来自 viewDidLoad 说)。对 layoutIfNeeded 的调用(如评论中建议的@Vrasidas)也应该有效,因为它也准备了布局。

The call to [_collectionView.collectionViewLayout prepareLayout] before the code above may help when you need to scroll the collection view which is not yet presented (from viewDidLoad say). The call to layoutIfNeeded (as @Vrasidas suggested in comments) should work too because it also prepares the layout.

这篇关于以编程方式滚动到UICollectionView中的补充​​视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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