Question

Here is my UICollectionViewFlowLayout subclass:

@implementation MyCollectionViewFlowLayout

- (id)init
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (void)setup
{
    self.itemSize = CGSizeMake(320, 320);
    self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.minimumInteritemSpacing = 0;
    self.minimumLineSpacing = 0;
    self.scrollDirection = UICollectionViewScrollDirectionVertical;
}


- (void)prepareLayout {
    [super prepareLayout];

}

- (CGSize)collectionViewContentSize {
    return self.itemSize;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray* allAttributesInRect = [super layoutAttributesForElementsInRect:rect];

    return allAttributesInRect;
}


- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes* attributes = [super layoutAttributesForItemAtIndexPath:indexPath];

    return attributes;
}

@end

When I do the following, the collection view will not scroll:

MyCollectionViewFlowLayout* flowLayout = [[MyCollectionViewFlowLayout alloc] init];

However if I do this:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(320, 320);
flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

Then the collection view scrolls fine. What am I doing wrong in the UICollectionViewFlowLayout subclass?

Was it helpful?

Solution

The problem is collectionViewContentSize

- (CGSize)collectionViewContentSize {
    return self.itemSize;
}

Returning itemSize here means that the entire contentSize for your collection view is only the size of one item. Try removing that code, or changing it to

- (CGSize)collectionViewContentSize {
    [super collectionViewContentSize];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top