【问题标题】:How do you set inner spacing in a collection view cell with 2 cells per row?如何在每行 2 个单元格的集合视图单元格中设置内部间距?
【发布时间】:2018-05-06 20:39:07
【问题描述】:

我已经坚持了两个星期了。到处搜索,仍然找不到解决方案。我想让细胞更靠近中间。还有另一个集合视图,每行有 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

}

【问题讨论】:

    标签: swift uicollectionview collectionview


    【解决方案1】:

    请阅读评论,不要忘记将您的手机设置为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
            }
    
    
    
        }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】: