1

I have a collection view which is delegate and datasource of itself, and with a layout defined like this:

- (id)initWithCoder:(NSCoder *)aDecoder{ 
    self = [super initWithCoder:aDecoder];
    if (self){
        self.dataSource = self;
        self.delegate = self;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 8, 0, 8);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.footerReferenceSize = CGSizeMake(1, 70);
        layout.itemSize = CGSizeMake(60, 70);
        layout.minimumInteritemSpacing = 4.0f;
        [self setCollectionViewLayout:layout];
    }
    return self;
}

The code is run correctly except for the minimuminteritemspacing.

The result is a collectionview where the items in every section don't have any spacing at all.

The minimum spacing value I set should be adjusted upward, not downward, correct?

So why the result I see is like the one in this screen? (I colored every collectionitem so that is clear there is no spacing)

CollectionView

For further detail, here are the frames I'm logging, as you can see the third item starts exactly after the second one (145 pixels = 85(previousitem.x) + 60(previousitem.width)) with no spacing.

2014-04-22 10:25:49.954 App[15172:60b] self <PhotoIndexCollectionViewCell: 0x16576630; baseClass = UICollectionViewCell; frame = (8 0; 60 70); layer = <CALayer: 0x165777a0>>

2014-04-22 10:25:49.955 App[15172:60b] self <PhotoIndexCollectionViewCell: 0x16578380; baseClass = UICollectionViewCell; frame = (85 0; 60 70); layer = <CALayer: 0x16578520>>

2014-04-22 10:25:49.955 App[15172:60b] self <PhotoIndexCollectionViewCell: 0x16586620; baseClass = UICollectionViewCell; frame = (145 0; 60 70); layer = <CALayer: 0x165866c0>>

What am I misunderstanding here?

I even tried adding this method which is actually called for every section, but the result is the same.

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4.0f;
}
Axy
  • 374
  • 2
  • 15
  • please read this answer http://stackoverflow.com/questions/17229350/cell-spacing-in-uicollectionview . i think it will help you. – stosha Apr 22 '14 at 02:26
  • I tried that, but still not working. I set up a minimum of 20 pixel to see if something will come up, but still the cells have no spacing. – Axy Apr 22 '14 at 05:07

2 Answers2

3

You can use following Delegate methods to set spacing:

// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

 //    NSLog(@"SETTING SIZE FOR ITEM AT INDEX %ld", (long)indexPath.row);
       CGSize mElementSize = CGSizeMake(150, 36);
       return mElementSize;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
       return 3.0;
}

 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
      return 7.0;
  }

// Layout: Set Edges
 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
   // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
      return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}
Chitra Khatri
  • 1,260
  • 2
  • 14
  • 31
  • I have all these methods already implemented, with my values of course, and still no result. – Axy Apr 22 '14 at 04:40
  • http://stackoverflow.com/questions/17229350/cell-spacing-in-uicollectionview and http://stackoverflow.com/questions/13017257/how-do-you-determine-spacing-between-cells-in-uicollectionview-flowlayout check this link – Chitra Khatri Apr 22 '14 at 09:09
  • I'm accepting your answer, because in my case, the spacing was not working due to a mistake in the Interface Builder. So actually the correct way is of course with the delegate methods you're pointing at. Thanks – Axy Jan 26 '15 at 05:35
  • 1
    @Axy: What was the mistake? I'm facing the same issue – W.K.S Apr 05 '17 at 09:14
0

As specified in the apple documentation, the space may be adjusted upward - But it looks that it may also be adjusted downward. I honestly don't really understand why and don't have time to investigate this right now ( I'm facing the problem, and have to finish my work ;) ) - It may simply be a bug in the Apple layout, because it is presented to be so simple

https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout/1617706-minimuminteritemspacing?changes=latest_minor

Anyway I see two ways to solve this :

1 - Compute the collection container bounds ( in the viewDidLayout function? ) by adding all items widths ( if width is not fixed ) or set it to ( itemWidth + minimumSpace ) * numberOfItems + minimumSpace ( for first padding )

2 - The radical way ( the one I have chosen ) : I embed my cell content in a view that is insetted by the amount I want. It just can't fail.

Cheers all

Moose
  • 2,607
  • 24
  • 23
  • Something is not quite right. Can you tell me where the documentation states that the space between cells may be less than the one requested? It is a minimumInteritemSpacing so I expect it to be "not less than.. " – Axy Nov 10 '17 at 10:55
  • Yes. My bad... "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." - I'm wrong, it's not downward, but upward. However, I suspect there is a bug in the Apple Layout.. – Moose Nov 10 '17 at 11:12
  • I've made some edits to my answer - I hope it is clearer. - One sure thing, the solution 2 is fast and works without headaches. – Moose Nov 10 '17 at 11:22