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)
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;
}
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
}