ios - 将 UICollectionView 滚动到节标题 View

标签 ios iphone uicollectionview

下面的代码滚动到 UICollectionView 中的右侧单元格,但在我的例子中,部分标题 View 隐藏在 UINavigationBar 后面。我相信我应该使用 scrollRectToVisible 而我的问题是,当 numberOfRows 在给定的 section 是可变的。

- (void)scrollToPricing:(NSUInteger)row {

    [self.collectionView scrollToItemAtIndexPath:
      [NSIndexPath indexPathForItem:0 
                          inSection:row] 
                   atScrollPosition:UICollectionViewScrollPositionTop 
                           animated:YES];
}

最佳答案

似乎所有的答案都过于复杂。这对我有用:

let attributes = self.collectionView.collectionViewLayout.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: NSIndexPath(forItem: 0, inSection: section))

self.collectionView.setContentOffset(CGPointMake(0, attributes!.frame.origin.y - self.collectionView.contentInset.top), animated: true)

swift 3:

if let attributes = collectionView.collectionViewLayout.layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: section)) {
    collectionView.setContentOffset(CGPoint(x: 0, y: attributes.frame.origin.y - collectionView.contentInset.top), animated: true)
}

关于ios - 将 UICollectionView 滚动到节标题 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22100227/

相关文章:

ios - UIImageView.appearance 覆盖了 UISegmentedControl.appearance

ios - UITextField 文字跳转

iphone - 鼠标悬停就像ios中的感觉

objective-c - 这应该自动发布吗?或者有没有办法手动完成?

android - Cardboard SDK Unity - 原生和共享属性的方法?

iphone - Xcode 4 - 重命名/删除文件,但出现 'file not found' 错误

iphone - 在应用程序设置中设置 iPhone 应用程序语言有效,但应用程序名称不会更改

ios - 如果数据源计数可能发生变化,是否可以同时使用 reloadItemsAtIndexPaths 和 reloadData?

ios - UICollectionViewFlowLayout minimumInteritemSpacing 不起作用

objective-c - UICollectionView 删除按钮