25

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?

John Doe
  • 811
  • 4
  • 14
  • 26

8 Answers8

32
//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))
}
Yagnesh Dobariya
  • 2,241
  • 19
  • 29
  • 2
    @ZacharyGover can you back up this "claim" with a creditable source? Most of Apples modules are still written in Objective-C, and it's still widely used today. – JAL Feb 26 '16 at 14:54
  • 1
    You do know that I said almost becoming obsolete? Yes some things are still written around Objective-C, although as Apple is updating Swift they are rewriting it all. Although that still means the majority of developers are writing in Swift. My main point that I was trying to get across is to give both answers, as you have now done. Because soon enough it will be obsolete, and people in the future won't be able to understand it if they're new to programming, when they look at this post for help. – ZZZZtop Feb 26 '16 at 14:59
30

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 .

Himan Dhawan
  • 894
  • 5
  • 23
  • Now issue can be in UIcollectionview cell ..... check out the constrains in UICollectionview cell ...... there should not be anytime of padding , rest i need to go through your code – Himan Dhawan Feb 26 '16 at 18:00
  • 1
    In story board there are two sections one is collectionview and under collectionview there is collectionview flow layout ....did u check min spacing in both ? – Himan Dhawan Feb 26 '16 at 18:06
16

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

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 2, bottom: 10, right: 2)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.scrollDirection = .horizontal
collectionView!.collectionViewLayout = layout
Vinu David Jose
  • 2,569
  • 1
  • 21
  • 38
Manny
  • 229
  • 3
  • 3
  • You are my hero. – Baran Jan 03 '18 at 20:47
  • One more point needs to add : Make sure that collectionview Scroll direction is in "Horizontal" for fixing horizontal cell gap. Otherwise minimumInteritemSpacing may not work.(Tested in Xcode 11.2.1 swift 4.2) – Zeesha Jan 16 '20 at 09:30
4

Its simple

let layout = contactsCollectionView.collectionViewLayout as? UICollectionViewFlowLayout
layout?.minimumLineSpacing = 8
MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
jamal zare
  • 1,037
  • 12
  • 13
3
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
        }
    }
Gabriel
  • 213
  • 5
  • 10
BM LADVA
  • 31
  • 3
3

Swift 5 Programmatically

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 collectionView Block.

Shubham Mishra
  • 1,303
  • 13
  • 24
1

If you create CollectionView In StoryBoard or Xib,Go to StoryBoard or Xib and set these Values from

enter image description here

MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
0

(Swift 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.
majid khan
  • 31
  • 3