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

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

概述我试图添加UICollectionView到ViewController,我需要有3个单元格“每行”,单元格之间没有空格(它应该看起来像一个网格)。单元格宽度应该是屏幕大小的三分之一,因此我认为layout.item宽度应该是相同的。但是我得到这个: 如果我减小那个大小(例如7或8像素),它是更好的,但行中的第三个单元格不是完全可见的,我仍然有空白空间(顶部&底部,左&右)。 class View 我试图添加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 = 0layout.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);}

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

总结

以上是内存溢出为你收集整理的swift – 如何设置单元格间距和UICollectionView – UICollectionViewFlowLayout大小比率?全部内容,希望文章能够帮你解决swift – 如何设置单元格间距和UICollectionView – UICollectionViewFlowLayout大小比率?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1060333.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存