Remove space between sections in collectionview

2019-03-11 04:38发布

问题:

How to adjust the spacing between sections of collection view.

回答1:

Header height can be adjusted by adjusting the params of the collection view layout. Following is the code which works perfectly fine.

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{
    if ([[sectionHeaderArray objectAtIndex:section] boolValue]) {
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
      return UIEdgeInsetsZero;
}


回答2:

You can use the method to implement this:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{
    //{top, left, bottom, right}

    if ([[sectionHeaderStatusArray objectAtIndex:section] boolValue]) {
        return UIEdgeInsetsMake(23, 19, 46, 14);
    }

      return UIEdgeInsetsZero;
}


回答3:

This is a layout issue, thus the answer will be in whatever layout you're using for the collection view. If you're using UICollectionViewFlowLayout then you'll want to set the sectionInset. e.g.

self.collectionView.collectionViewLayout.sectionInset = UIEdgeInsetsZero;


回答4:

Try this:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{
    if ([[sectionHeaderArray objectAtIndex:section] boolValue]) {
        return UIEdgeInsetsMake(top, left, bottom, right);
    }
      return UIEdgeInsetsZero;
}