简体   繁体   English

在集合视图中增加单元格之间的间距大小

[英]Increase size of spacing between cells in collection view

I want different spacing between my cells inside my collectionview. 我想在collectionview中的单元格之间使用不同的间距。

My collectionview contains only one section. 我的收藏集视图仅包含一个部分。 I know it's possible to change the spacing between sections but I'll like between cells. 我知道可以更改部分之间的间距,但我会喜欢单元格之间的间隔。

I would like the spacing to be greater between the centered cell and its sides, and that the spacing of the other cells do not move. 我希望居中的单元格及其侧面之间的间距更大,并且其他单元格的间距不要移动。 Basically, my white circle does not touch the edges of neighboring cells without changing all the spacing. 基本上,我的白色圆圈在不更改所有间距的情况下不会接触相邻单元的边缘。

Image of my problem here : 我的问题的图片在这里:

我的问题的图片在这里

There is a solution ? 有解决办法吗?

您可以在视图控制器中实现此方法: collectionView:layout:minimumInteritemSpacingForSectionAtIndex

You can try creating 3 different sections. 您可以尝试创建3个不同的部分。

  1. 1st Section - section containing cells before the larger cell, with inter item spacing as 10 第一部分 -包含较大单元格之前的单元格的单元格,单元inter item spacing as 10
  2. 2nd Section - section containing the larger cell, with section insets as (top: 0, left: 20, bottom: 0, right: 20) 第二部分 -包含较大单元格的section insets as (top: 0, left: 20, bottom: 0, right: 20)section insets as (top: 0, left: 20, bottom: 0, right: 20)
  3. 3rd section - section containing cells after the larger cell, with inter item spacing as 10 第三部分 -包含较大单元格之后的单元格的部分, inter item spacing as 10

You can change the values as per your requirements. 您可以根据需要更改值。

Here is what the code says: 代码是这样的:

func numberOfSections(in collectionView: UICollectionView) -> Int
{
    return 3
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    if section == 1
    {
        return 1
    }
    return 2
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    if indexPath.section == 1
    {
        cell.backgroundColor = UIColor.red
    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
    if indexPath.section == 1
    {
        return CGSize(width: 100, height: 100)
    }
    return CGSize(width: 82, height: 82)
}

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
    if section == 1
    {
        return UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    }
    return .zero
}

Screenshot: 截图:

在此处输入图片说明

Let me know if you still face any issues. 让我知道您是否仍然遇到任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM