Vap*_*olf 21 iphone ipad ios uicollectionview
我UICollectionView用来分段显示照片.每个部分都有一个补充视图作为标题,并通过以下方法提供:viewForSupplementaryElementOfKind.
我的侧面有一个擦洗器,允许用户从一个部分跳到另一个部分.现在我正在滚动到该部分中的第一项使用scrollToItemAtIndexPath:atScrollPosition:animated:,但我真正想要的是滚动collectionView,以便该部分的标题位于屏幕的顶部,而不是第一个单元格.我没有看到一个明显的方法来做到这一点.你们中有人有工作吗?
我想我可以滚动到该部分的第一项,然后通过补充高度加上项目和标题之间的偏移来抵消它(如果它归结为那个(有一种方法可以滚动到contentView的点坐标)).但是,如果有一种更简单的方法,我想知道.
谢谢.
Ser*_*nko 38
你不能用scrollToItemAtIndexPath:atScrollPosition:animated它.
希望他们将scrollToSupplementaryElementOfKind:atIndexPath:在未来添加一种新方法,但就目前而言,唯一的方法是直接操作contentOffset.
下面的代码显示了如何使用FlowLayout将标题滚动到垂直顶部.您可以对水平滚动执行相同操作,或将此想法用于其他布局类型.
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];
Run Code Online (Sandbox Code Playgroud)
请注意,如果您有非零contentInset(如在iOS 7中,当滚动视图在条形下方展开时),您需要从中减去它offsetY,如图所示.同样的sectionInset.
更新:
该代码假定布局处于准备好的"有效"状态,因为它使用它来计算偏移量.当集合视图显示其内容时,将准备布局.
[_collectionView.collectionViewLayout prepareLayout]当您需要滚动尚未显示的集合视图时(viewDidLoad例如),上面代码之前的调用可能会有所帮助.对layoutIfNeeded(在评论中建议的@Vrasidas)的调用也应该有效,因为它也准备了布局.
Swift中的解决方案,
let section: = 0 // Top
if let cv = self.collectionView {
cv.layoutIfNeeded()
let indexPath = NSIndexPath(forItem: 1, inSection: section)
if let attributes = cv.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) {
let topOfHeader = CGPointMake(0, attributes.frame.origin.y - cv.contentInset.top)
cv.setContentOffset(topOfHeader, animated:true)
}
}
Run Code Online (Sandbox Code Playgroud)
Gene De Lisa的道具:http://www.rockhoppertech.com/blog/scroll-to-uicollectionview-header/
| 归档时间: |
|
| 查看次数: |
10860 次 |
| 最近记录: |