ios - 在Swift中删除Uicollectionview中的多余空间-LMLPHP
代码:

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))
}

最佳答案

根据你的要求做这件事,希望能帮助你得到一个主意。

  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

关于ios - 在Swift中删除Uicollectionview中的多余空间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42856119/

10-15 07:54