Skip to content

Instantly share code, notes, and snippets.

@knitish90
Forked from vigorouscoding/DateFlowLayout.h
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save knitish90/49eaa0b0f0c91db15b14 to your computer and use it in GitHub Desktop.

Select an option

Save knitish90/49eaa0b0f0c91db15b14 to your computer and use it in GitHub Desktop.

Revisions

  1. vigorouscoding created this gist Mar 13, 2013.
    5 changes: 5 additions & 0 deletions DateFlowLayout.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    #import <UIKit/UIKit.h>

    @interface DateFlowLayout : UICollectionViewFlowLayout

    @end
    94 changes: 94 additions & 0 deletions DateFlowLayout.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    @implementation DateFlowLayout

    -(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.headerReferenceSize = CGSizeMake(50, 50);
    self.minimumInteritemSpacing = 0;
    self.minimumLineSpacing = 0;
    self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

    return self;
    }

    -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    UICollectionView * const cv = self.collectionView;
    CGPoint const contentOffset = cv.contentOffset;

    NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
    if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
    [missingSections addIndex:layoutAttributes.indexPath.section];
    }
    }
    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
    if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
    [missingSections removeIndex:layoutAttributes.indexPath.section];
    }
    }

    [missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];

    UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];

    [answer addObject:layoutAttributes];

    }];

    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {

    if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {

    NSInteger section = layoutAttributes.indexPath.section;
    NSInteger numberOfItemsInSection = [cv numberOfItemsInSection:section];

    NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
    NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section];

    UICollectionViewLayoutAttributes *firstCellAttrs = [self layoutAttributesForItemAtIndexPath:firstCellIndexPath];
    UICollectionViewLayoutAttributes *lastCellAttrs = [self layoutAttributesForItemAtIndexPath:lastCellIndexPath];


    if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {
    CGFloat headerHeight = CGRectGetHeight(layoutAttributes.frame);
    CGPoint origin = layoutAttributes.frame.origin;
    origin.y = MIN(
    MAX(contentOffset.y, (CGRectGetMinY(firstCellAttrs.frame) - headerHeight)),
    (CGRectGetMaxY(lastCellAttrs.frame) - headerHeight)
    );

    layoutAttributes.zIndex = 1024;
    layoutAttributes.frame = (CGRect){
    .origin = origin,
    .size = layoutAttributes.frame.size
    };
    } else {
    CGFloat headerWidth = CGRectGetWidth(layoutAttributes.frame);
    CGPoint origin = layoutAttributes.frame.origin;
    origin.x = MIN(
    MAX(contentOffset.x, (CGRectGetMinX(firstCellAttrs.frame) - headerWidth)),
    (CGRectGetMaxX(lastCellAttrs.frame) - headerWidth)
    );

    layoutAttributes.zIndex = 1024;
    layoutAttributes.frame = (CGRect){
    .origin = origin,
    .size = layoutAttributes.frame.size
    };
    }

    }
    }

    return answer;
    }

    -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBound {
    return YES;
    }

    @end