【问题标题】:How to remove the gap between header and cell of UICollectionView?如何消除 UICollectionView 的 header 和 cell 之间的间隙?
【发布时间】:2021-01-18 00:09:18
【问题描述】:

我有一个UICollectionView,它有一个标题和一个单元格。 我想消除标题和单元格之间的间隙.. 如何在swift 中做到这一点?

这是我的看法...

我为collectionView 以及标题和单元格添加了背景颜色..

请看截图。

【问题讨论】:

  • 你可以尝试将automaticallyAdjustsScrollViewInsets设置为false
  • 我试过了..它不起作用@Cheng-YuHsu
  • 能否请您为标题和单元格视图添加背景颜色并发布快照,以及您的集合视图的大小检查器设置的快照,这将有助于我们更好地了解问题。
  • 是的,我添加了背景颜色...请查看更改。 @RDC
  • 你使用什么自动调整大小或大小类?

标签: ios swift uicollectionview


【解决方案1】:

使用UICollectionViewFlowLayoutsection Inset属性

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
// Here you can set according your requirement

更多参考:http://www.brianjcoleman.com/tutorial-collection-view-using-swift/

【讨论】:

    【解决方案2】:

    使用UICollectionViewFlowLayout的属性sectionInset

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 1;
    

    在斯威夫特中:

    var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 1
    

    【讨论】:

    • 我认为 OP 想要 Swift 语法,你能把语法翻译成 Swift 吗?
    【解决方案3】:

    附加故事板屏幕截图。根据您的情节提要设置并检查。希望您的问题能得到解决。

    【讨论】:

    • 我做了更改.. 差距仍然存在.. 最小间距默认为 10.. 我将其更改为 0.. 但差距很大。
    • 检查它是否没有以编程方式设置。
    • 不,它没有在任何地方以编程方式设置。
    【解决方案4】:

    我忘记将 Inset 部分设置为零。 它解决了我的问题。 谢谢大家。

    【讨论】:

      【解决方案5】:

      试试这个:

         override func viewWillAppear(animated: Bool) {
      self.automaticallyAdjustsScrollViewInsets = false;
      }
      

      在 UICollectionViewDelegate 上:

       let k_cViewPadding = 8.0
      
      func collectionView(collectionView: UICollectionView,
              layout collectionViewLayout: UICollectionViewLayout,
              sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
                  let cellDiffference:CGFloat = CGFloat(k_cViewPadding*4)
                  return(CGSizeMake((collectionView.frame.size.width-cellDiffference)/3, (collectionView.frame.size.width-cellDiffference)/3))
          } 
      

      【讨论】:

        【解决方案6】:

        这是我的示例代码,您必须将 UIEdgeInsets 设置为间距。

        private let collectionViewLayout: UICollectionView = {
            let layout = UICollectionViewFlowLayout()
            layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
            layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
            let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
            collectionView.translatesAutoresizingMaskIntoConstraints = false
            collectionView.showsHorizontalScrollIndicator = false
            collectionView.showsVerticalScrollIndicator = false
            collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
            collectionView.register(TopExpertCustomCell.self, forCellWithReuseIdentifier: "TopExpertCollectionViewCell")
            collectionView.backgroundColor = UIColor(hex: "#F1F1F1")
            return collectionView
        }()
        

        【讨论】:

          【解决方案7】:

          自定义你的 UIEdgeInsetsMake:

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

          【讨论】: