【问题标题】:Remove space between sections in collectionview删除collectionview中部分之间的空间
【发布时间】:2015-04-07 19:39:48
【问题描述】:

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

【问题讨论】:

  • 你试过collectionView:layout:insetForSectionAtIndex:吗?

标签: ios iphone uicollectionview collectionview uicollectionreusableview


【解决方案1】:

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

self.collectionView.collectionViewLayout.sectionInset = UIEdgeInsetsZero;

【讨论】:

  • 然后提供更多信息。你用的是什么布局?我认为你需要调查它是什么布局。
  • 对于流式布局,如果让 layout = collectionView.collectionViewLayout 为? UICollectionViewFlowLayout { layout.sectionInset = UIEdgeInsets.zero }
【解决方案2】:

您可以使用该方法来实现:

- (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】:

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

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

    【讨论】:

      【解决方案4】:

      试试这个:

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

      【讨论】:

        【解决方案5】:

        这是 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)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2021-12-15
          • 1970-01-01
          • 2017-04-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-05
          • 2019-06-13
          相关资源
          最近更新 更多