ios 如何更改 UICollectionView 中单元格之间的空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35654129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:36:23  来源:igfitidea点击:

How to change space between cells in UICollectionView?

iosswiftuicollectionviewuicollectionviewcell

提问by John Doe

I want to reduce the size between cells in row. Now it looks like:

我想减小行中单元格之间的大小。现在它看起来像:

enter image description here

在此处输入图片说明

I'm trying this, for reduce the size between them:

我正在尝试这个,以减少它们之间的大小:

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 2, bottom: 10, right: 2)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0

but it doesn't help me. What I do wrong?

但这对我没有帮助。我做错了什么?

回答by Yagnesh Dobariya

//Objective c
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        return CGSizeMake((collectionView.frame.size.width/3)-20, 100);
    }

// Swift 2.0

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake((collectionView.frame.size.width / 3) - 20, 100)
}

// Swift 3.0
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: CGFloat((collectionView.frame.size.width / 3) - 20), height: CGFloat(100))
}

回答by Himan Dhawan

Go to the storyboard , right click on collection view and enter image description here

转到故事板,右键单击集合视图并 在此处输入图片说明

And Check your minimum spacing between the cell and reduce it to zero .

并检查单元格之间的最小间距并将其减少到零。

回答by Manny

On your code, you have to create a IBOutlet to your collection view, and then assign the collectionViewLayout as such:

在您的代码中,您必须为您的集合视图创建一个 IBOutlet,然后将 collectionViewLayout 分配如下:

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 2, bottom: 10, right: 2)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView!.collectionViewLayout = layout

回答by jamal zare

Its simple

这很简单

let layout = contactsCollectionView.collectionViewLayout as? UICollectionViewFlowLayout
layout?.minimumLineSpacing = 8

回答by BM LADVA

extension ViewController: UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let collectionWidth = collectionView.bounds.width
            return CGSize(width: collectionWidth, height: collectionWidth/2)
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return 0
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
            return 100
        }
    }

回答by Shubham Mishra

Swift 5 Programmatically

Swift 5 以编程方式

lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal

        //Provide Width and Height According to your need
        let width = UIScreen.main.bounds.width / 4
        let height = UIScreen.main.bounds.height / 10
        layout.itemSize = CGSize(width: width, height: height)

        //Make Space as Zero
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0

        return UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    }()

You can provide much more customization inside the lazy collectionViewBlock.

您可以在lazy collectionViewBlock内提供更多自定义。

回答by Pengyu Zhang

If you create CollectionViewIn StoryBoardor Xib,Go to StoryBoardor Xiband set these Values from

如果您创建CollectionViewInStoryBoardXib,转到StoryBoardXib并从

enter image description here

在此处输入图片说明

回答by majid khan

(Swift 4)

(斯威夫特 4)

  • Click on collection View in storyboard.
  • on right hand side, click size inspector.
  • manage and alter the cell size value and you'll figure it out by yourself.
  • 单击故事板中的集合视图。
  • 在右侧,单击尺寸检查器。
  • 管理和更改单元格大小值,您将自己弄清楚。