GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Sticky header example · lyandy/CSStickyHeaderFlowLayout@0187994

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • CSStickyHeaderFlowLayout.podspec
  • Classes
    • CSStickyHeaderFlowLayout.h
    • CSStickyHeaderFlowLayout.m
  • Project
    • CSStickyHeaderFlowLayoutDemo.xcworkspace
      • contents.xcworkspacedata
    • CSStickyHeaderFlowLayoutDemo/Base.lproj
      • Main.storyboard
    • Podfile.lock
    • Podfile
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎CSStickyHeaderFlowLayout.podspec‎

Copy file name to clipboardExpand all lines: CSStickyHeaderFlowLayout.podspec
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ Pod::Spec.new do |s|
1919
s.license = 'MIT'
2020
s.author = { "Jamz Tang" => "[email protected]" }
2121
s.source = { :git => "http://EXAMPLE/NAME.git", :tag => s.version.to_s }
22-
23-
# s.platform = :ios, '5.0'
24-
# s.ios.deployment_target = '5.0'
22+
s.platform = :ios, '7.0'
23+
s.ios.deployment_target = '7.0'
2524
# s.osx.deployment_target = '10.7'
2625
s.requires_arc = true
2726

Collapse file

‎Classes/CSStickyHeaderFlowLayout.h‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* This file is part of the Canvas package.
3+
* (c) Canvas <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
#import <UIKit/UIKit.h>
10+
11+
extern NSString *const CSStickyHeaderParallexHeader;
12+
13+
@interface CSStickyHeaderFlowLayout : UICollectionViewFlowLayout
14+
15+
@property (nonatomic) CGSize parallexHeaderReferenceSize;
16+
@property (nonatomic) CGSize parallexHeaderMinimumReferenceSize;
17+
@property (nonatomic) BOOL disableStickyHeaders;
18+
19+
@end
Collapse file

‎Classes/CSStickyHeaderFlowLayout.m‎

Copy file name to clipboard
+153Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* This file is part of the Canvas package.
3+
* (c) Canvas <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
#import "CSStickyHeaderFlowLayout.h"
10+
11+
NSString *const CSStickyHeaderParallexHeader = @"CSStickyHeaderParallexHeader";
12+
13+
@implementation CSStickyHeaderFlowLayout
14+
15+
- (void)prepareLayout {
16+
[super prepareLayout];
17+
}
18+
19+
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
20+
{
21+
// The rect should compensate the header size
22+
CGRect adjustedRect = rect;
23+
adjustedRect.origin.y -= self.parallexHeaderReferenceSize.height;
24+
25+
NSMutableArray *allItems = [[super layoutAttributesForElementsInRect:adjustedRect] mutableCopy];
26+
27+
NSMutableDictionary *headers = [[NSMutableDictionary alloc] init];
28+
NSMutableDictionary *lastCells = [[NSMutableDictionary alloc] init];
29+
__block BOOL visibleParallexHeader;
30+
31+
[allItems enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
32+
UICollectionViewLayoutAttributes *attributes = obj;
33+
34+
CGRect frame = attributes.frame;
35+
frame.origin.y += self.parallexHeaderReferenceSize.height;
36+
attributes.frame = frame;
37+
38+
NSIndexPath *indexPath = [(UICollectionViewLayoutAttributes *)obj indexPath];
39+
if ([[obj representedElementKind] isEqualToString:UICollectionElementKindSectionHeader]) {
40+
[headers setObject:obj forKey:@(indexPath.section)];
41+
} else if ([[obj representedElementKind] isEqualToString:UICollectionElementKindSectionFooter]) {
42+
// Not implemeneted
43+
} else {
44+
NSIndexPath *indexPath = [(UICollectionViewLayoutAttributes *)obj indexPath];
45+
46+
UICollectionViewLayoutAttributes *currentAttribute = [lastCells objectForKey:@(indexPath.section)];
47+
48+
// Get the bottom most cell of that section
49+
if ( ! currentAttribute || indexPath.row > currentAttribute.indexPath.row) {
50+
[lastCells setObject:obj forKey:@(indexPath.section)];
51+
}
52+
53+
if ([indexPath item] == 0 && [indexPath section] == 0) {
54+
visibleParallexHeader = YES;
55+
}
56+
}
57+
}];
58+
59+
NSUInteger numberOfSections = [self.collectionView.dataSource numberOfSectionsInCollectionView:self.collectionView];
60+
61+
// Create the attributes for the Parallex header
62+
if (visibleParallexHeader && ! CGSizeEqualToSize(CGSizeZero, self.parallexHeaderReferenceSize) && numberOfSections > 0) {
63+
UICollectionViewLayoutAttributes *currentAttribute = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:CSStickyHeaderParallexHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
64+
CGRect frame = currentAttribute.frame;
65+
frame.size.width = self.parallexHeaderReferenceSize.width;
66+
frame.size.height = self.parallexHeaderReferenceSize.height;
67+
68+
CGRect bounds = self.collectionView.bounds;
69+
CGFloat maxY = CGRectGetMaxY(frame);
70+
71+
// make sure the frame won't be negative values
72+
CGFloat y = MIN(maxY - self.parallexHeaderMinimumReferenceSize.height, bounds.origin.y + self.collectionView.contentInset.top);
73+
CGFloat height = MAX(0, -y + maxY);
74+
75+
currentAttribute.frame = (CGRect){
76+
frame.origin.x,
77+
y,
78+
frame.size.width,
79+
height,
80+
};
81+
82+
currentAttribute.zIndex = -1;
83+
84+
[allItems addObject:currentAttribute];
85+
}
86+
87+
if ( ! self.disableStickyHeaders) {
88+
[lastCells enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
89+
NSIndexPath *indexPath = [obj indexPath];
90+
NSNumber *indexPathKey = @(indexPath.section);
91+
92+
UICollectionViewLayoutAttributes *header = headers[indexPathKey];
93+
// CollectionView automatically removes headers not in bounds
94+
if ( ! header) {
95+
header = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
96+
atIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.section]];
97+
98+
if (header) {
99+
[allItems addObject:header];
100+
}
101+
}
102+
[self updateHeaderAttributes:header lastCellAttributes:lastCells[indexPathKey]];
103+
}];
104+
}
105+
106+
return allItems;
107+
}
108+
109+
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
110+
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
111+
CGRect frame = attributes.frame;
112+
frame.origin.y += self.parallexHeaderReferenceSize.height;
113+
attributes.frame = frame;
114+
return attributes;
115+
}
116+
117+
- (CGSize)collectionViewContentSize {
118+
CGSize size = [super collectionViewContentSize];
119+
size.height += self.parallexHeaderReferenceSize.height;
120+
return size;
121+
}
122+
123+
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
124+
{
125+
return YES;
126+
}
127+
128+
#pragma mark Helper
129+
130+
- (void)updateHeaderAttributes:(UICollectionViewLayoutAttributes *)attributes lastCellAttributes:(UICollectionViewLayoutAttributes *)lastCellAttributes
131+
{
132+
CGRect currentBounds = self.collectionView.bounds;
133+
attributes.zIndex = 1024;
134+
attributes.hidden = NO;
135+
136+
CGPoint origin = attributes.frame.origin;
137+
138+
CGFloat sectionMaxY = CGRectGetMaxY(lastCellAttributes.frame) - attributes.frame.size.height;
139+
CGFloat y = CGRectGetMaxY(currentBounds) - currentBounds.size.height + self.collectionView.contentInset.top;
140+
141+
CGFloat maxY = MIN(MAX(y, attributes.frame.origin.y), sectionMaxY);
142+
143+
// NSLog(@"%.2f, %.2f, %.2f", y, maxY, sectionMaxY);
144+
145+
origin.y = maxY;
146+
147+
attributes.frame = (CGRect){
148+
origin,
149+
attributes.frame.size
150+
};
151+
}
152+
153+
@end
Collapse file

‎Project/CSStickyHeaderFlowLayoutDemo.xcworkspace/contents.xcworkspacedata‎

Copy file name to clipboardExpand all lines: Project/CSStickyHeaderFlowLayoutDemo.xcworkspace/contents.xcworkspacedata
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎Project/CSStickyHeaderFlowLayoutDemo/Base.lproj/Main.storyboard‎

Copy file name to clipboardExpand all lines: Project/CSStickyHeaderFlowLayoutDemo/Base.lproj/Main.storyboard
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
1313
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1414
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
15-
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="YVv-HD-P4u">
15+
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="YVv-HD-P4u" customClass="CSStickyHeaderFlowLayout">
1616
<size key="itemSize" width="320" height="80"/>
1717
<size key="headerReferenceSize" width="0.0" height="50"/>
1818
<size key="footerReferenceSize" width="0.0" height="0.0"/>
@@ -69,7 +69,7 @@
6969
</collectionViewController>
7070
<placeholder placeholderIdentifier="IBFirstResponder" id="Cud-9j-Qcf" userLabel="First Responder" sceneMemberID="firstResponder"/>
7171
</objects>
72-
<point key="canvasLocation" x="27" y="119"/>
72+
<point key="canvasLocation" x="80" y="76"/>
7373
</scene>
7474
<!--Navigation Controller-->
7575
<scene sceneID="EeI-38-cgl">
@@ -84,6 +84,7 @@
8484
</navigationController>
8585
<placeholder placeholderIdentifier="IBFirstResponder" id="wFw-aA-dqz" userLabel="First Responder" sceneMemberID="firstResponder"/>
8686
</objects>
87+
<point key="canvasLocation" x="469" y="76"/>
8788
</scene>
8889
</scenes>
8990
<simulatedMetricsContainer key="defaultSimulatedMetrics">
Collapse file

‎Project/Podfile‎

Copy file name to clipboard
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1+
platform :ios, '7.0'
12
pod "CSStickyHeaderFlowLayout", :path => "../CSStickyHeaderFlowLayout.podspec"
2-
3-
target "Demo" do
4-
end
5-
6-
target "DemoTests" do
7-
end
Collapse file

‎Project/Podfile.lock‎

Copy file name to clipboard
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
PODS:
2+
- CSStickyHeaderFlowLayout (0.1.0)
3+
4+
DEPENDENCIES:
5+
- CSStickyHeaderFlowLayout (from `../CSStickyHeaderFlowLayout.podspec`)
6+
7+
EXTERNAL SOURCES:
8+
CSStickyHeaderFlowLayout:
9+
:path: ../CSStickyHeaderFlowLayout.podspec
10+
11+
SPEC CHECKSUMS:
12+
CSStickyHeaderFlowLayout: 51514d6588f4e984bfb9719c1ae147ccbc60cf00
13+
14+
COCOAPODS: 0.29.0

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.