10

I've got two problems with my UICollectionView:

  • minimumInteritemSpacing doesn't work
  • it overflows horizontally on iOS 6

I set up the layout like this:

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(70.0f, 70.0f);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumLineSpacing = 0.0f;
layout.minimumInteritemSpacing = 0.0f;

_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
// I set the size of _collectionView in layoutSubviews:
// _collectionView.frame = self.bounds;

_collectionView.contentInset = UIEdgeInsetsMake(8.0f, 8.0f, 8.0f, 8.0f);

The image shows the result on iOS 6 (on iOS 7 there is no overflow, but the spacing between columns is still not zero)

enter image description here

I tried this solution https://gist.github.com/OliverLetterer/5583087, but it doesn't fix anything in my case.

GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
HiveHicks
  • 2,294
  • 5
  • 28
  • 42

2 Answers2

18

From the documentation for the minimumInterItemSpacing property:

For a horizontally scrolling grid, this value represents the minimum spacing between items in the same column. This spacing is used to compute how many items can fit in a single line, but after the number of items is determined, the actual spacing may possibly be adjusted upward.

The flow layout will evenly space cells across its width, with a spacing of no smaller than the minimum you set. If you don't want the spacing, you'll need to implement your own layout.

The iOS 6 overflow issue I'm not sure about. Try dropping support for iOS 6 ;)

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thx, u r right, I can not see this tip in Xcode quick help... and I forget to check the document.. – Xueshi Dec 11 '15 at 03:54
  • Am I missing something or your answer is contradicting the emphasis you gave on docs? "the actual spacing may possibly be adjusted upward" means the "first size" may be smaller, which contradicts "your flow layout will evenly space cells across its width, with a spacing of no smaller than the minimum you set". – henrique Jul 13 '21 at 18:37
14

"line spacing" can mean the space between vertical lines.

Say you have a ordinary single line horizontal collection view.

(Eg, the whole view is simply 50 high, and the items are simply 50x50.)

As user @matt has explained

  • a horizontal collection view has columns of cells

  • the "lines" as Apple means it are the vertical "lines" (!) of cells

Thus:

  • in the case of a simple horizontal collection view with one row,

  • what Apple names the "line" spacing is - indeed - the spacing between items

Thus surprisingly in a simple horizontal collection view with one row, to set the gap between items, it's just:

l.minimumLineSpacing = 6  // Apple means "vertical scan lines" by "lines"

(minimumInteritemSpacing is completely meaningless in a normal simple horizontal collection view with one row.)

This finally explains why there are 100 pages on the internet asking why minimumInteritemSpacing just doesn't work. Fantastic tip by user @matt

Fattie
  • 27,874
  • 70
  • 431
  • 719