删除collectionview中各节之间的空格

时间:2015-02-07 19:39:39

标签: ios iphone uicollectionview collectionview uicollectionreusableview

如何调整集合视图各部分之间的间距。

enter image description here

5 个答案:

答案 0 :(得分:16)

您可以使用该方法实现此目的:

- (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;
}

答案 1 :(得分:4)

可以通过调整集合视图布局的参数来调整页眉高度。以下是运行良好的代码。

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

答案 2 :(得分:2)

这是一个布局问题,因此答案将在您用于集合视图的任何布局中。如果您使用的是UICollectionViewFlowLayout,那么您需要设置sectionInset。 e.g。

self.collectionView.collectionViewLayout.sectionInset = UIEdgeInsetsZero;

答案 3 :(得分:2)

尝试一下:

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

答案 4 :(得分:0)

这是Swift 4.2版本。

这使您可以为不同部分设置各种插图配置。

/// 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)
    }
}
相关问题