【问题标题】:Remove Extra Space in Collection View in Swift在 Swift 中删除集合视图中的额外空间
【发布时间】:2017-08-08 22:16:55
【问题描述】:

代码:

let reuseIdentifier = "cell" 
var items = ["1", "2", "3", "4","5","6","7","8", "9", "10", "11","12","13","14"]

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text = self.items[indexPath.item]
    cell.backgroundColor = UIColor.red
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: CGFloat((collectionView.frame.size.width / 2) - 8), height: CGFloat(250))
}

【问题讨论】:

  • 图片不可见
  • 可以贴截图吗?
  • 您应该解释您遇到的问题并展示您尝试解决的问题。水平轴上有多余的空间吗?纵轴?细胞之间?部分之间?请详细说明,以便您可以帮助我们。
  • @ShyamShukla :将您的屏幕截图上传到imgur.com 等图片上传网站并在此处分享链接。
  • 我的截图链接在这里。ibb.co/jeXYov

标签: ios iphone swift uicollectionview


【解决方案1】:

根据您的要求执行此操作,希望它可以帮助您获得一个想法。

  let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 10, left: 1, bottom: 5, right: 0) // according to your requirements
            layout.minimumInteritemSpacing = 1 // according to your requirements
            layout.minimumLineSpacing = 1// according to your requirements

如果您需要更多帮助或者这对您不起作用,您也可以通过link 来帮助您实现目标。点击这个link

【讨论】:

    【解决方案2】:
     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
     {
    return CGSize(width: CGFloat((collectionView.frame.size.width / 2) - 8), height: (collectionView.frame.size. height / 3) - 8))
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 2021-12-26
      • 1970-01-01
      • 2021-12-08
      相关资源
      最近更新 更多