Skip to content

Instantly share code, notes, and snippets.

@leeprobert
Created March 15, 2013 12:22
Show Gist options
  • Save leeprobert/5169529 to your computer and use it in GitHub Desktop.
Save leeprobert/5169529 to your computer and use it in GitHub Desktop.
UICollectionViewFlowLayout class that positions the CollectionView Header and Footer views horizontally to the section items. This was used to create a Calendar Week view with one cell per section (representing the day of the week) and the Section header to the left of it and a footer to the right. The other sections were the other days of the w…
//
// CollectionViewCalendarFlowLayout.m
#import "CollectionViewCalendarFlowLayout.h"
#define HEADER_WIDTH 200.0f
#define FOOTER_WIDTH 20.0f
#define SECTION_MARGIN_X 0.0f
#define PADDING_X 0.0f
#define WEEKEND_CELL_HEIGHT 100.0f
@interface CollectionViewCalendarFlowLayout (){
UIInterfaceOrientation _orientation;
}
-(void)initLayoutProperties;
@end
@implementation CollectionViewCalendarFlowLayout
-(void)prepareLayout {
[super prepareLayout];
[self initLayoutProperties];
}
-(void)initLayoutProperties {
_orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect viewFrame = self.collectionView.frame;
CGFloat cellHeight = (viewFrame.size.height/7)-2;
self.itemSize = CGSizeMake(cellHeight, cellHeight); // width will change based on visible view
/*
If you make the default header/footer size 0.0 it doesn't attempt to add the views.
If you don't offset the default size we get a gap between items.
*/
self.headerReferenceSize = CGSizeMake(1.0f, 1.0f);
self.footerReferenceSize = CGSizeMake(1.0f, 1.0f);
//self.sectionInset = UIEdgeInsetsMake(-1.0f, 0.0f, -1.0f, 0.0f);
self.sectionInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
self.minimumLineSpacing = 0.0f;
self.minimumInteritemSpacing = 0.0f;
}
/*
Return attributes for items
*/
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* array = [super layoutAttributesForElementsInRect:rect];
for(UICollectionViewLayoutAttributes* attributes in array) {
UICollectionViewLayoutAttributes *suppViewAttributes;
if([[attributes representedElementKind] isEqualToString:UICollectionElementKindSectionHeader]){
suppViewAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[attributes indexPath]];
[attributes setFrame:[suppViewAttributes frame]];
}else if([[attributes representedElementKind] isEqualToString:UICollectionElementKindSectionFooter]){
suppViewAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:[attributes indexPath]];
[attributes setFrame:[suppViewAttributes frame]];
}else{
CGFloat w = rect.size.width-((SECTION_MARGIN_X*2)+HEADER_WIDTH+FOOTER_WIDTH+(PADDING_X*2));
CGFloat h = self.itemSize.height;
attributes.frame = CGRectMake(SECTION_MARGIN_X+HEADER_WIDTH+PADDING_X, attributes.frame.origin.y, w, h);
}
}
return array;
}
-(UICollectionViewLayoutAttributes*)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
/*
First get the attributes for the cell item
*/
UICollectionViewLayoutAttributes* cellAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
UICollectionViewLayoutAttributes* supViewAttributes;
BOOL isHeader = [kind isEqualToString:UICollectionElementKindSectionHeader];
/*
We want our header view to align-left to the cell and the footer to sit on the right
*/
supViewAttributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
CGRect cellRect = [cellAttributes frame];
if(isHeader){
[supViewAttributes setFrame:CGRectMake(SECTION_MARGIN_X, cellRect.origin.y, HEADER_WIDTH, cellRect.size.height)];
}else{
CGRect collectionViewRect = self.collectionView.bounds;
CGFloat footerXpos = collectionViewRect.size.width-(FOOTER_WIDTH+SECTION_MARGIN_X+PADDING_X);
[supViewAttributes setFrame:CGRectMake(footerXpos, cellRect.origin.y, FOOTER_WIDTH, cellRect.size.height)];
}
return supViewAttributes;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment