UICollectionView单元格间距不再起作用

时间:2018-05-29 20:40:58

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我已经在这个应用程序上工作了几天,我正在设计一个UICollectionView看起来像一个网格,在单元格之间没有间距。在我找到this answer并将其实施到我自己的程序中后,它的工作非常出色。我处理了一些其他文件并更新了我的计算机,然后我回来测试这个应用程序只是为了看看我在哪里,并且由于某种原因在单元格之间存在水平间距。我已经看了几个类似于这个问题的其他问题,但我只是想知道如何解决这个问题以及为什么当我没有改变任何与布局相关的问题时就会发生这种情况。

以下是现在的样子:

Here's what it looks like now

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

var model = BoardModel()
var board = Board()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    collectionView.delegate = self
    collectionView.dataSource = self

    board = model.newGame(4, 4)

    // Remove the spacing between the cells in collectionView
    // Also make the cells the correct size to form a square/rectangle
    let screenSize: CGRect! = collectionView.bounds
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: screenSize.width/CGFloat(board.columns), height: screenSize.width/CGFloat(board.columns))
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    collectionView!.collectionViewLayout = layout

    // Center the collectionView
    collectionView.contentInset.top = max((collectionView.frame.height - collectionView.contentSize.height) / 5, 0)

}

我使用collectionView边界而不是UIScreen.mainScreen()。bounds的原因是因为我的collectionView在所有边缘周围有20个间距。

2 个答案:

答案 0 :(得分:0)

你能试试吗

let width = UIScreen.main.bounds.width - 40 // convert it if it's not swift 4
let height = UIScreen.main.bounds.height - 40  

//

或实施

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

     return CGSize.init(width:  , height:  )
}

答案 1 :(得分:0)

import UIKit

class ViewController: UIViewController {
    fileprivate let sectionInsets = UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0)
    fileprivate let itemsPerRow: CGFloat = 2

  override func viewDidLoad() {
        super.viewDidLoad()
}

}
// MARK: UICollectionViewDelegateFlowLayout

extension ViewController : UICollectionViewDelegateFlowLayout {


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
        let availableWidth = view.frame.width - paddingSpace
        let widthPerItem = availableWidth / itemsPerRow
        let heightPerItem = widthPerItem + 21

        return CGSize(width: widthPerItem, height: heightPerItem)
    }

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{
        return sectionInsets.left
    }

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

        return 0
    }

}