Question

I have an Extended UICollectionFlowLayout. This vertically centres the UIcollectionViewCell by translating the attribute.frame by required amount and also shifting the visible Rect of collection view to show the transformed cells.

This works perfectly fine in ios7. However in ios6 the visible Rect of collection view does not change , hence forth cells are shown shifted but clipped.

Eg : -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect rect = (0,0,320,500) and I shift cells by 200 then cells with start showing from (0,0,320,200) to (0,0,320,500) and those below 500 will be clipped. Any reason why this would happen in ios6 when it work perfectly in iOS7 ?

@implementation VerticallyCenteredFlowLayout

    -(id)init
    {
        if (!(self = [super init])) return nil;
        [self setMinimumLineSpacing:5.0];
        [self setMinimumInteritemSpacing:0.0];
        [self setItemSize:CGSizeMake(10, 10)];
        [self setSectionInset:UIEdgeInsetsMake(0, 11, 11, 11)];
        [self setScrollDirection:UICollectionViewScrollDirectionVertical];
        return self;
    }

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

        UICollectionViewLayoutAttributes* att = [array lastObject];
        if (att){
            CGFloat lastY = att.frame.origin.y + att.frame.size.height;
            CGFloat diff = self.collectionView.frame.size.height - lastY;
            if (diff > 0){
                for (UICollectionViewLayoutAttributes* a in array){
                    a.frame = CGRectMake(a.frame.origin.x, a.frame.origin.y + diff/2, a.frame.size.width, a.frame.size.height) ;
                }
            }
        }
        return array;
    }
Was it helpful?

Solution

The contentSize was not being automatically Adjusted in ios6.

Overiding following method in VerticallyCenteredFlowLayout Class fixed the issue

-(CGSize)collectionViewContentSize {
    CGSize size = [super collectionViewContentSize];
    if (size.height < MIN(_maxHeight,self.collectionView.frame.size.height)) {
        size.height = MIN(_maxHeight,self.collectionView.frame.size.height);
    }
    return size;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top