swift - 如何设置单元格间距和 UICollectionView - UICollectionViewFlowLayout 大小比例?

标签 swift uicollectionview uicollectionviewcell uicollectionviewlayout

我正在尝试将 UICollectionView 添加到 ViewController,我需要“每行”有 3 个单元格,单元格之间没有空格(它应该看起来像一个网格).单元格宽度应为屏幕大小的三分之一,因此我认为 layout.item 宽度应该相同。但后来我明白了:

layout.itemSize width = screenSize.width/3

如果我减小该尺寸(例如 78 像素),效果会更好,但是行中的第三个单元格并不完全可见,但我仍然有空白(顶部和底部,以及左侧和右侧)。

enter image description here

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    var collectionView: UICollectionView?
    var screenSize: CGRect!
    var screenWidth: CGFloat!
    var screenHeight: CGFloat!

    override func viewDidLoad() {
        super.viewDidLoad()

        screenSize = UIScreen.mainScreen().bounds
        screenWidth = screenSize.width
        screenHeight = screenSize.height

        // Do any additional setup after loading the view, typically from a nib
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
        layout.itemSize = CGSize(width: screenWidth / 3, height: screenWidth / 3)
        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView!.dataSource = self
        collectionView!.delegate = self
        collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView!.backgroundColor = UIColor.greenColor()
        self.view.addSubview(collectionView!)
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
        cell.backgroundColor = UIColor.whiteColor()
        cell.layer.borderColor = UIColor.blackColor().CGColor
        cell.layer.borderWidth = 0.5
        cell.frame.size.width = screenWidth / 3
        cell.frame.size.height = screenWidth / 3

        cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
        return cell
    }
}

最佳答案

添加这两行

layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0

所以你有:

   // Do any additional setup after loading the view, typically from a nib.
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
        layout.itemSize = CGSize(width: screenWidth/3, height: screenWidth/3)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        collectionView!.collectionViewLayout = layout

这将删除所有空格并为您提供网格布局:

enter image description here

如果您希望第一列的宽度等于屏幕宽度,请添加以下函数:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if indexPath.row == 0
    {
        return CGSize(width: screenWidth, height: screenWidth/3)
    }
    return CGSize(width: screenWidth/3, height: screenWidth/3);

}

网格布局现在看起来像(我还为第一个单元格添加了蓝色背景): enter image description here

关于swift - 如何设置单元格间距和 UICollectionView - UICollectionViewFlowLayout 大小比例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28325277/

相关文章:

ios - Swift 无法将选项卡栏添加到选项卡栏 View Controller ?变灰了?

swift - Swift 3 GCD API 更改后的 dispatch_once

ios - 如何从collectionview中被点击的单元格中获取信息

ios - 为什么 dequeueReusableCell(withReuseIdentifier :for:) is nil outside the cellForItemAtIndexPath function scope?

ios - 点击时更改 UICollectionViewCell 上的 ImageView 图像

ios - 如何在旋转时调整 CollectionView 的特定单元格的大小?

ios - 调整包含 UITextView 的 UICollectionViewCell 以适应 contentSize

ios - xCode 8,swift 3,使用 LoginManager 的 Facebook SDK 登录必须点击两次才能打开 FB 登录表单

iphone - UICollectionReusableView 使用动画更改标题高度

ios - 找不到字体(Swift)