-
Notifications
You must be signed in to change notification settings - Fork 15
/
StretchyHeaderCollectionViewLayout.m
53 lines (37 loc) · 1.46 KB
/
StretchyHeaderCollectionViewLayout.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// StretchyHeaderCollectionViewLayout.m
// StretchyHeaders
//
// Created by Nick Jensen on 12/26/13.
// Copyright (c) 2013 Nick Jensen. All rights reserved.
//
#import "StretchyHeaderCollectionViewLayout.h"
@implementation StretchyHeaderCollectionViewLayout
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
- (UICollectionViewScrollDirection)scrollDirection {
return UICollectionViewScrollDirectionVertical;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
UICollectionView *collectionView = [self collectionView];
UIEdgeInsets insets = [collectionView contentInset];
CGPoint offset = [collectionView contentOffset];
CGFloat minY = -insets.top;
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
if (offset.y < minY) {
CGSize headerSize = [self headerReferenceSize];
CGFloat deltaY = fabsf(offset.y - minY);
for (UICollectionViewLayoutAttributes *attrs in attributes) {
if ([attrs representedElementKind] == UICollectionElementKindSectionHeader) {
CGRect headerRect = [attrs frame];
headerRect.size.height = MAX(minY, headerSize.height + deltaY);
headerRect.origin.y = headerRect.origin.y - deltaY;
[attrs setFrame:headerRect];
break;
}
}
}
return attributes;
}
@end