ios - 在 Swift 中删除 Collection View 中的额外空间

标签 ios iphone swift uicollectionview

Desired Output like this image

代码:

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 中删除 Collection View 中的额外空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42856119/

相关文章:

iOS 将 View 底部的约束添加到容器中心

ios - UIAlertController 不应显示

ios - 将泛型类作为参数传递给 swift 中的函数

iphone - 在相机叠加层中创建我们自己的裁剪矩形

ios - 类型 'PlaySoundsViewController' 的值没有成员 'recordedAudio'

ios - 访问 IOS 数组和字典中包含的值

ios - 在 Xcode 5 中开发的 iPhone 应用程序,在 iPad 上运行时不显示图标和启动图像

iphone - 在 ios 中将字符串中的两个值加在一起

swift - swift move 吃 bean

swift - 给定一个 Swift `Any` 类型,我可以确定它是否是 `Optional` 吗?