如何在每行 2 个单元格的集合视图单元格中设置内部间距?

How do you set inner spacing in a collection view cell with 2 cells per row?

我已经坚持了两个星期了。到处搜索,仍然找不到解决方案。我想让细胞更靠近中间。还有另一个集合视图,每行 3 个单元格,我需要用它做同样的事情。谢谢

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

return CGSize(width: 153, height: 241)

}

//

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

return UIEdgeInsetsMake(10, 11, 10, 11)

}

//

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

return 0

}

//

func collectionView(_ collectionView: UICollectionView, layout    collectionViewLayout: UICollectionViewLayout, maximumLineSpacingForSectionAt section: Int) -> CGFloat {

return 0

}

请阅读评论,不要忘记在 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICollectionViewCell

设置您的手机
 extension ViewController :UICollectionViewDelegate , UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
         func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 100 // data source
          }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICollectionViewCell
               cell.backgroundColor = .red
              return cell
           }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

            // 3 cell same size in each row space between them 4
            let spaceBetweenCell :CGFloat = 4.0  // if you change this parmters you have to change minimumInteritemSpacingForSectionAt ,
            let screenWidth = UIScreen.main.bounds.size.width - CGFloat(2 * spaceBetweenCell)
            let totalSpace = spaceBetweenCell * 2.0;  // there is 2 between  3 item
            return CGSize(width: (screenWidth - totalSpace)/3, height: (screenWidth-totalSpace)/3)

        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return 4.0 // space between sections
        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
            return 4.0 // space between items in row
        }



    }

如果您对单元格大小满意并且只想将它们移近一些,请调整您的 leftright 插图(它们是 [=13= 的第二个和第四个输入) ]):

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(10, 16, 10, 16)
}