Search code examples
iosobjective-cuicollectionviewuicollectionviewlayout

iOS UICollectionViewCells Layout with intersection


I try to make UICollectionView with cells, that intersect and partially overlay each other as it is done at screenshot: Layout 1 This layout was reached by setting

self.minimumLineSpacing = -100;

at my UICollectionViewFlowLayout subclass. When I scroll down, everything is OK. I see what I want. But when I scroll up, I see another behaviour, not like I expected: Layout 2 So my question is: how can I make my layout look as at the first screen regardless scroll view direction. Note: I have to support both iOS 6 and 7.

Thanks very much for any advices and any help.


Solution

  • Found another sollution to solve this problem. We need to use UICollectionViewFlowLayout subclass.

    @interface MyFlowLayout : UICollectionViewFlowLayout
    @end
    
    @implementation MyFlowLayout
    
    - (void)prepareLayout {
        [super prepareLayout];
        // This allows us to make intersection and overlapping
        self.minimumLineSpacing = -100;
    }
    
    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
        NSArray *layoutAttributes = [super layoutAttributesForElementsInRect:rect];
        for (UICollectionViewLayoutAttributes *currentLayoutAttributes in layoutAttributes) {
            // Change zIndex allows us to change not only visible position, but logic too
            currentLayoutAttributes.zIndex = currentLayoutAttributes.indexPath.row;
        }
        return layoutAttributes;
    }
    
    @end
    

    Hope that helps someone else.