16

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

在此处输入图像描述

4

5 回答 5

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;
}
于 2015-06-16T15:57:08.390 回答
10

这是 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)
    }
}
于 2019-04-09T01:02:42.493 回答
8

可以通过调整集合视图布局的参数来调整标题高度。以下是完美运行的代码。

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{
    if ([[sectionHeaderArray objectAtIndex:section] boolValue]) {
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
      return UIEdgeInsetsZero;
}
于 2018-12-06T08:25:22.910 回答
3

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

self.collectionView.collectionViewLayout.sectionInset = UIEdgeInsetsZero;
于 2015-02-08T01:14:06.747 回答
2

尝试这个:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{
    if ([[sectionHeaderArray objectAtIndex:section] boolValue]) {
        return UIEdgeInsetsMake(top, left, bottom, right);
    }
      return UIEdgeInsetsZero;
}
于 2018-12-27T09:57:15.837 回答