How to adjust the spacing between sections of collection view.

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;
}
Mayank Birani
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;
}
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;
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;
}
Here's the Swift 4.2 version.
This lets you set various inset configurations for different sections.
/// Formats the insets for the various headers and sections.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if section == 0 {
// No insets for header in section 0
return UIEdgeInsets.zero
} else {
// Normal insets for collection
return UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
}
}
来源:https://stackoverflow.com/questions/28386506/remove-space-between-sections-in-collectionview