Skip to content

Instantly share code, notes, and snippets.

@wess
Created January 8, 2013 15:07
Show Gist options
  • Save wess/4484487 to your computer and use it in GitHub Desktop.
Save wess/4484487 to your computer and use it in GitHub Desktop.
Wow, collectionview layout all wrong?
//
// BJUPortraitLayout.h
// BizjournalsUniversal
//
// Created by Wess Cope on 1/7/13.
// Copyright (c) 2013 ACBJ. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BJUPortraitLayout : UICollectionViewFlowLayout
@end
///////////////////////////////////////////////////////////////////////////
//
// BJUPortraitLayout.m
// BizjournalsUniversal
//
// Created by Wess Cope on 1/7/13.
// Copyright (c) 2013 ACBJ. All rights reserved.
//
#import "BJUPortraitLayout.h"
#import "Archimedes.h"
static CGFloat const kBJUCollectionLayoutTopHeight = 300.0f;
@interface BJUPortraitLayout()
//- (CGRect)frameForViewAtIndexPath:(NSIndexPath *)indexPath;
@end
@implementation BJUPortraitLayout
- (id)init
{
self = [super init];
if (self)
{
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
return self;
}
//- (void)prepareLayout
//{
//
//}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
// NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
NSInteger numberOfSections = self.collectionView.numberOfSections;
NSMutableArray *attributes = [NSMutableArray new];
for(int i = 0; i < numberOfSections; i++)
{
NSIndexPath *headlinePath = [NSIndexPath indexPathForRow:0 inSection:i];
NSIndexPath *adPath = [NSIndexPath indexPathForRow:1 inSection:i];
NSIndexPath *subArticleOnePath = [NSIndexPath indexPathForRow:2 inSection:i];
NSIndexPath *subArticleTwoPath = [NSIndexPath indexPathForRow:3 inSection:i];
NSIndexPath *subArticleThreePath = [NSIndexPath indexPathForRow:4 inSection:i];
NSIndexPath *subArticleFourPath = [NSIndexPath indexPathForRow:5 inSection:i];
UICollectionViewLayoutAttributes *headlineAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:headlinePath];
UICollectionViewLayoutAttributes *adAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:adPath];
UICollectionViewLayoutAttributes *subArticleOneAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:subArticleOnePath];
UICollectionViewLayoutAttributes *subArticleTwoAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:subArticleTwoPath];
UICollectionViewLayoutAttributes *subArticleThreeAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:subArticleThreePath];
UICollectionViewLayoutAttributes *subArticleFourAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:subArticleFourPath];
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;
CGFloat subArticleWidth = (visibleRect.size.width / 2);
CGFloat subArticleHeight = ((visibleRect.size.height - kBJUCollectionLayoutTopHeight) / 2);
CGRect headlineFrame = CGRectMake(0.0f, 0.0f, visibleRect.size.width - kBJUCollectionLayoutTopHeight, kBJUCollectionLayoutTopHeight);
CGRect adFrame = CGRectMake(headlineFrame.size.width, 0.0f, kBJUCollectionLayoutTopHeight, kBJUCollectionLayoutTopHeight);
CGRect subArticleOneFrame = CGRectMake(0.0f, kBJUCollectionLayoutTopHeight, subArticleWidth, subArticleHeight);
CGRect subArticleTwoFrame = CGRectMake(subArticleWidth, kBJUCollectionLayoutTopHeight, subArticleWidth, subArticleHeight);
CGRect subArticleThreeFrame = CGRectMake(0.0f, kBJUCollectionLayoutTopHeight + subArticleHeight, subArticleWidth, subArticleHeight);
CGRect subArticleFourFrame = CGRectMake(subArticleWidth, kBJUCollectionLayoutTopHeight + subArticleHeight, subArticleWidth, subArticleHeight);
headlineAttributes.frame = headlineFrame;
adAttributes.frame = adFrame;
subArticleOneAttributes.frame = subArticleOneFrame;
subArticleTwoAttributes.frame = subArticleTwoFrame;
subArticleThreeAttributes.frame = subArticleThreeFrame;
subArticleFourAttributes.frame = subArticleFourFrame;
[attributes addObjectsFromArray:@[headlineAttributes, adAttributes, subArticleOneAttributes, subArticleTwoAttributes, subArticleThreeAttributes, subArticleFourAttributes]];
}
return [attributes copy];
}
//- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
//{
// return
//}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment