I'm trying to subclass the UICollectionViewFlowLayout
so that I can eliminate the space in between cells, because the flow layout only allows you to set a minimumInterItemSpacing
, and not a maximumInterItemSpacing
. I need a collectionView that scrolls vertically with dynamic cell heights and has no space in between the cells. Here is what I have tried in my subclass of UICollectionViewFlowLayout
:
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for(int i = 1; i < [answer count]; i++) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
NSInteger maximumSpacing = 0;
NSInteger origin = prevLayoutAttributes.frame.origin.y + prevLayoutAttributes.frame.size.height;
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.height < self.collectionViewContentSize.height) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.y = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return answer;
}
While this does seem to eliminate interitem spacing, it also produces odd side effects. Some cells end up way too tall, and others jumping around in size. Also, if I scroll really fast, some cells never show up at all. Does anybody know how I could best implement this seemingly simple thing?
Thanks