【问题标题】:How to center a UICollectionView when it is tapped?轻按时如何将 UICollectionView 居中?
【发布时间】:2013-07-26 14:47:06
【问题描述】:

我有一个UICollectionView,上面有很多UICollectionViewCells。我想在点击时将单元格滚动到UICollectionView 的中心。我担心的是,即使我点击最后一个或顶部的单元格,它也应该移动到集合视图的中心。

我尝试设置contentInsets 和偏移量,但它们似乎不起作用。我想我必须在选择时更改内容大小,并在滚动开始时将其更改回原始大小。

【问题讨论】:

  • 检查我编辑的答案,我添加了一些更改

标签: ios objective-c uiscrollview uicollectionview


【解决方案1】:

设置 contentInsets 应该在第一个和最后一个单元格周围留出一些额外的空间:

CGFloat collectionViewHeight = CGRectGetHeight(collectionView.bounds);
[collectionView
  setContentInset:
   UIEdgeInsetsMake(collectionViewHeight/2, 0, collectionViewHeight/2, 0) ];
  // nb, those are top-left-bottom-right

你应该打电话后:

[collectionView scrollToItemAtIndexPath:selectedItemPath
    atScrollPosition:UICollectionViewScrollPositionCenteredVertically
    animated:YES];

传递正确的滚动位置很重要:UICollectionViewScrollPositionCenteredVertically

这应该正确地居中点击项目。

编辑

真的很奇怪,但是设置 UIEdgeInsets 为集合视图方法 scrollToItemAtIndexPath 后不能正常工作,所以我做了一些修改:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat collectionViewHeight = CGRectGetHeight(self.collectionView.frame);
    [collectionView setContentInset:UIEdgeInsetsMake(collectionViewHeight / 2, 0, collectionViewHeight / 2, 0)];

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    CGPoint offset = CGPointMake(0,  cell.center.y - collectionViewHeight / 2);
    [collectionView setContentOffset:offset animated:YES];
}

对我来说很好用。

【讨论】:

  • 错字:UIEdgeInstetsMake 应该是 UIEdgeInsetsMake
  • 我得到“UICollectionView 没有可见界面声明选择器 setContentInsets”
  • @shim,哦,是的,这是一个错字 - 应该是 setContentInset。
  • scrollToItemAtIndexPath + 使用 Inset 的问题仅在单元格可见时才会发生。我最终使用了 Mikhail 和 scrollToItemAtIndexPath 的两种解决方法,具体取决于单元格是否可见。 (解决方法仅适用于可见单元格,因为 cellForItemAtIndexPath 将返回 nil 到不可见单元格)
  • 是的,由于某种原因,设置 contentInsetscrollToItemAtIndexPath 不能一起工作。我认为这是一个错误
【解决方案2】:

这个错误似乎是由滚动视图的contentInsetUICollectionViewFlowLayout 之间的不良交互引起的。在我的测试中,设置layout.sectionInset 而不是collectionView.contentInset 消除了这个问题。

根据上面接受的答案,我将消除解决方法,并进行更改:

[collectionView setContentInset:UIEdgeInsetsMake(collectionViewHeight / 2, 0, collectionViewHeight / 2, 0)];

[layout setSectionInset:UIEdgeInsetsMake(collectionViewHeight / 2, 0, collectionViewHeight / 2, 0)];

【讨论】:

  • 修复了 Mikhail 解决方法的惊人解决方案。谢谢!
【解决方案3】:

您可以使用scrollToItemAtIndexPath 方法将选中(点击)单元格置于 UICollectionView 的中心位置

[collectionView scrollToItemAtIndexPath:indexPath
                       atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                               animated:true];

您可以将UICollectionViewScrollPositionCenteredVertically 用于垂直居中位置

【讨论】:

    【解决方案4】:

    Swift 3 用于滚动和居中屏幕以使点击的项目可见的解决方案:

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
    }
    

    这不会在collectionView! 上方或下方添加插图!

    【讨论】:

    • 如果我想居中对齐第一个和最后一个选定的单元格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    相关资源
    最近更新 更多