Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A simple sample that shows how to make suplementary views stick.
- Loading branch information
Derrick J. Hathaway
committed
Jun 4, 2013
1 parent
de35663
commit ed0a723
Showing
11 changed files
with
256 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
StickyViews.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// MyHeaderOrFooterView.h | ||
// StickyViews | ||
// | ||
// Created by derrick on 6/4/13. | ||
// Copyright (c) 2013 derrh. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface MyHeaderOrFooterView : UICollectionReusableView | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// MyHeaderOrFooterView.m | ||
// StickyViews | ||
// | ||
// Created by derrick on 6/4/13. | ||
// Copyright (c) 2013 derrh. All rights reserved. | ||
// | ||
|
||
#import "MyHeaderOrFooterView.h" | ||
|
||
@implementation MyHeaderOrFooterView | ||
|
||
- (id)initWithFrame:(CGRect)frame | ||
{ | ||
self = [super initWithFrame:frame]; | ||
if (self) { | ||
// Initialization code | ||
} | ||
return self; | ||
} | ||
|
||
/* | ||
// Only override drawRect: if you perform custom drawing. | ||
// An empty implementation adversely affects performance during animation. | ||
- (void)drawRect:(CGRect)rect | ||
{ | ||
// Drawing code | ||
} | ||
*/ | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// MyLayout.h | ||
// StickyViews | ||
// | ||
// Created by derrick on 6/4/13. | ||
// Copyright (c) 2013 derrh. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface MyLayout : UICollectionViewFlowLayout | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// MyLayout.m | ||
// StickyViews | ||
// | ||
// Created by derrick on 6/4/13. | ||
// Copyright (c) 2013 derrh. All rights reserved. | ||
// | ||
|
||
#import "MyLayout.h" | ||
|
||
@implementation MyLayout | ||
|
||
|
||
/* in order to make the header and footer float (or stick to the | ||
top and bottom of the screen), we need to invalidate the layout | ||
on bounds change (when scrolling). | ||
*/ | ||
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds | ||
{ | ||
return YES; | ||
} | ||
|
||
/* override this method so that you can add your header and footer | ||
to the list of UICollectionReusableView's that will be requested | ||
from your dataSource. | ||
*/ | ||
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect | ||
{ | ||
NSMutableArray *allItems = [[super layoutAttributesForElementsInRect:rect] mutableCopy]; | ||
|
||
__block BOOL headerFound = NO; | ||
__block BOOL footerFound = NO; | ||
[allItems enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | ||
if ([[obj representedElementKind] isEqualToString:UICollectionElementKindSectionHeader]) { | ||
headerFound = YES; | ||
[self updateHeaderAttributes:obj]; | ||
} else if ([[obj representedElementKind] isEqualToString:UICollectionElementKindSectionFooter]) { | ||
footerFound = YES; | ||
[self updateFooterAttributes:obj]; | ||
} | ||
}]; | ||
|
||
|
||
// Flow layout will remove items from the list if they are supposed to be off screen, so we add them | ||
// back in in those cases. | ||
if (!headerFound) { | ||
[allItems addObject:[self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:[allItems count] inSection:0]]]; | ||
} | ||
if (!footerFound) { | ||
[allItems addObject:[self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:[allItems count] inSection:0]]]; | ||
} | ||
|
||
return allItems; | ||
} | ||
|
||
/* now provide the layout attributes for your floating header/footer. */ | ||
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:indexPath]; | ||
attributes.size = CGSizeMake(self.collectionView.bounds.size.width, 44); | ||
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { | ||
[self updateHeaderAttributes:attributes]; | ||
} else { | ||
[self updateFooterAttributes:attributes]; | ||
} | ||
return attributes; | ||
} | ||
|
||
#pragma mark - make things stick | ||
|
||
- (void)updateHeaderAttributes:(UICollectionViewLayoutAttributes *)attributes | ||
{ | ||
CGRect currentBounds = self.collectionView.bounds; | ||
attributes.zIndex = 1; | ||
attributes.hidden = NO; | ||
CGFloat yCenterOffset = currentBounds.origin.y + attributes.size.height/2.f; | ||
attributes.center = CGPointMake(CGRectGetMidX(currentBounds), yCenterOffset); | ||
} | ||
|
||
- (void)updateFooterAttributes:(UICollectionViewLayoutAttributes *)attributes | ||
{ | ||
CGRect currentBounds = self.collectionView.bounds; | ||
attributes.zIndex = 1; | ||
attributes.hidden = NO; | ||
CGFloat yCenterOffset = currentBounds.origin.y + currentBounds.size.height - attributes.size.height/2.0f; | ||
attributes.center = CGPointMake(CGRectGetMidX(currentBounds), yCenterOffset); | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.