在单元格之间的 UICollectionView 中设置/覆盖填充 [英] Set/Override Padding In UICollectionView Between Cells

查看:33
本文介绍了在单元格之间的 UICollectionView 中设置/覆盖填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UICollectionView,但在获取单元格之间的填充时遇到了问题.理论上,我应该能够将屏幕除以 4,并且我可以获得一个包含 4 个图像的单元格大小,这些图像完美地占据了屏幕宽度.但是,它选择不这样做.相反,它创建了 3 个带有巨大填充的图像,如下所示:

I have a UICollectionView and I'm running into an issue with getting the padding between cells. In theory, I should be able to divide the screen by 4, and I can get a cellsize that has 4 images which perfectly take up the screen width. But, it chooses not to do that. Instead it creates 3 images with HUGE padding as shown below:

_cellSize = CGSizeMake(UIScreen.mainScreen().bounds.width/4,UIScreen.mainScreen().bounds.width/4)

所以我稍微减小了单元格大小以创建一些填充,这更接近但我需要大约一半的填充:

So I decreased the cell size a bit to create some padding, and this was closer but I need about half this padding:

_cellSize = CGSizeMake(UIScreen.mainScreen().bounds.width/4.4,UIScreen.mainScreen().bounds.width/4.4)

所以我试着让单元格变大一点,它会滚动到下一行并再次放置 3 个带有巨大填充的项目.

So I tried making the cells a bit bigger and it rolls over to the next row and puts 3 items with HUGE padding again.

_cellSize = CGSizeMake(UIScreen.mainScreen().bounds.width/4.3,UIScreen.mainScreen().bounds.width/4.3)

我可以指定单元格之间的填充而不是决定如果填充低于x"量,它会创建一个新行吗?就像我可以在新行之前设置阈值填充说,0?

Can I specify the padding between cells without it deciding that if the padding is below "x" amount that it creates a new row instead? Like can I set the threshold padding before a new row to say, 0?

我想让它变得这么薄:

推荐答案

您只需要进行正确的计算,包括您想要的边缘空间以及单元格之间的空间.例如,如果你想要 3 个单元格,边缘和单元格之间有 5 个点,你应该有这样的东西,

You only need to do the correct calculation that includes the spaces you want to the edges as well as the space between the cells. For instance, if you want 3 cells across with 5 points to the edges and between the cells, you should have something like this,

override func viewDidLoad() {
        super.viewDidLoad()
        var layout = self.collectionView.collectionViewLayout as UICollectionViewFlowLayout
        layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
        layout.minimumInteritemSpacing = 5; // this number could be anything <=5. Need it here because the default is 10.
        layout.itemSize = CGSizeMake((self.collectionView.frame.size.width - 20)/3, 100) // 20 is 2*5 for the 2 edges plus 2*5 for the spaces between the cells
    }

这篇关于在单元格之间的 UICollectionView 中设置/覆盖填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆