在 UICollectionViewController 中控制单元格之间的间隙 [英] Controlling the gap between cells in UICollectionViewController

查看:23
本文介绍了在 UICollectionViewController 中控制单元格之间的间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单元格似乎有大约 20 点的差距,我想控制该值,但是我找到的有关此问题的每个答案都不起作用.

My cells seem to have a gap of about 20 points, I want to control that value, but every answer I found regarding this question does not work.

我试过了:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets.zero
}

没有任何有用的输出,还有:

without any useful output, also:

layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0

这两行也没有帮助.

请注意,我没有使用故事板!

Please note that I'm not using the storyboard!

推荐答案

从逻辑上讲,gap 值应该根据单元格的大小,编辑最小间距导致行应该如何显示.为了更清楚,请考虑(想象)以下场景:

Logically speaking, the gap value should be based on what's the cell size, editing the minimum spaces leads to how the row should be displayed. To make it more clear, consider (imagine) the following scenario:

每行需要显示2个单元格,每个单元格的宽度应该是屏幕宽度的一半,如果最小间距设置为零,输出将是:两个单元格都应该填满屏幕宽度.

it's required to display 2 cells for each row, each cell's width should be the half of the screen width, if the minimum spaces have been set to zeros, output will be: both cells should fill the screen width.

到目前为止一切都很好,但是如果单元格的宽度应该大于屏幕宽度的一半(让我们考虑它应该是宽度的一半 + 2 点)怎么办?输出将是:每行将只包含 1 个单元格,旁边有一个宽度为屏幕一半的间隙 - 2 点.

So far so good, but what if the cell's width should be more than the half of the screen width (let's consider that it should be the half of the width + 2 points)? the output will be: each row will contains only 1 cell, beside it a gap with the width of the half of screen - 2 points.

因此,解决方案确保通过实施collection View: layout: size For Item At Index Path: 正确.您可能需要查看 此问答 了解如何实现.

So, the solution is to make sure to set the appropriate size for the cells, by implementing collection​View:​layout:​size​For​Item​At​Index​Path:​ correctly. You might want to check this Q&A to know how you can achieve it.

这篇关于在 UICollectionViewController 中控制单元格之间的间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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