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

前端之家收集整理的这篇文章主要介绍了swift – 如何设置单元格间距和UICollectionView – UICollectionViewFlowLayout大小比率?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图添加UICollectionView到ViewController,我需要有3个单元格“每行”,单元格之间没有空格(它应该看起来像一个网格)。单元格宽度应该是屏幕大小的三分之一,因此我认为layout.item宽度应该是相同的。但是我得到这个:

如果我减小那个大小(例如7或8像素),它是更好的,但行中的第三个单元格不是完全可见的,我仍然有空白空间(顶部&底部,左&右)。

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
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
添加这2行
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,right: 0)
        layout.itemSize = CGSize(width: screenWidth/3,height: screenWidth/3)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        collectionView!.collectionViewLayout = layout

这将删除所有的空格,并给你一个网格布局:

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

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

}

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

原文链接:https://www.f2er.com/swift/321388.html

猜你在找的Swift相关文章