调整 collectionView.scrollToItem 以考虑插入?

Adjust collectionView.scrollToItem to consider inset?

我有时会像这样滚动到单元格的左侧:

collectionView.scrollToItem(
    at: IndexPath(row: 5, section: 0),
    at: .left, // TODO: Left ignores inset
    animated: true
)

这是 scrollToItem 实施之前的开始方式:

但是,当我尝试使用滚动到项目时,它会将单元格粘在边缘而不是考虑插图:

有没有简单的方法来修复 collectionView.scrollToItem 以容纳插图?

由于每个单元格的框架都考虑了它的部分插图,你也可以使用 scrollRectToVisible(_:animated:).

let cell = collectionView.cellForItem(at: indexPath)!
collectionView.scrollRectToVisible(cell.frame, animated: true)

由于这是我们正在处理的框架,您可以微调该框架的一些所需偏移量。

Objective-C

   /**
     Assumptions:
     1. Your collection view scrolls horizontally
     2. You are using UICollectionViewFlowLayout to layout you collection view

     @param indexPath IndexPath to scroll to
     @param animated Toggle animations
     */
    - (void)scrollToIndexPathPreservingLeftInset:(NSIndexPath *)indexPath animated:(BOOL)animated {
        UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
        CGFloat sectionLeftInset = layout.sectionInset.left;
        UICollectionViewLayoutAttributes *attri = [layout layoutAttributesForItemAtIndexPath:indexPath];
        [collectionView setContentOffset:CGPointMake(attri.frame.origin.x - sectionLeftInset, 0) animated:animated];
    }

Swift(语法上未验证)

func scroll(toIndexPathPreservingLeftInset indexPath: IndexPath, animated: Bool) {
    let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    let sectionLeftInset = layout.sectionInset.left
    var attri = layout.layoutAttributesForItem(at: aPath)
    collectionView.setContentOffset(CGPoint(x: (attri?.frame.origin.x - sectionLeftInset), y: 0), animated: animated)
}

Sample gif

如果您在 collectionView.contentInset 而不是 layout.sectionInset 中设置插图,则可以使用常规方法 collectionView.scrollToItem

像这样为您的 collectionView 设置 conentInset 值:

myCollectionView.contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)

然后添加这一行:

 if #available(iOS 11, *) {
            self.myCollectionView.contentInsetAdjustmentBehavior =     .never
        }

然后 scrolltoItem 通过尊重 collectionView 的插图来工作。

万一其他人无意中看到滚动到水平集合视图的开头,在考虑插入部分时,我能够通过这样做来实现它以下(在 Swift 5.2.4 中)。该解决方案利用了 UICollectionViews 是 UIScrollViews 的事实:

    (collectionView as UIScrollView).setContentOffset(.zero, animated: true)