UICollectionViewFlowLayout 左侧的间隙

Gap on left hand side of UICollectionViewFlowLayout

我正在使用 UICollectionViewFlowLayout 来显示网格视图,这就是我所看到的。

问题出在左侧的缝隙上。我对使用 UICollectionViewFlowLayout 有点陌生,所以我不知道如何消除那里的差距。

请帮忙

要减少左侧的 space,您可以在 UICollectionViewFlowLayout

上设置 sectionInset 属性
flowLayout.sectionInsets = UIEdgeInsets(top: 50.0, left: 0.0, bottom: 50.0, right: 0.0)

或者你可以实现这个方法:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 50.0, left: 0.0, bottom: 50.0, right: 0.0)
   }

If the delegate object does not implement the collectionView:layout:insetForSectionAtIndex: method, the flow layout uses the value in this property to set the margins for each section.

来源:Apple Documentation

尝试使用:

 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
  return UIEdgeInsetsMake(5, 5, 5, 5); //desired values
}

希望对您有所帮助。

我的第一个猜测是检查 sectionInsets。您可以直接在流布局上设置它们:

flowLayout.sectionInsets = UIEdgeInsets(...)

或通过 ~UICollectionViewDelegateFlowLayout` 方法:

optional func collectionView(_ collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    insetForSectionAtIndex section: Int) -> UIEdgeInsets {

}

Flow Layout Delegate Reference

Flow Layout Reference