I have a UICollectionView with a grey background color, however I want one of my sections to have a white background. I have discovered this was not as simple as I had hoped (i.e. the section having a attribute I could just set).
From looking at How to change background color of a whole section in UICollectionView? it was suggested to use a decoration view and this question UICollectionView Decoration View has helped me change the section color by subclassing UICollectionReusableView
and UICollectionViewFlowLayout
.
My issue is that I cannot get the section height (which is dynamic) to set the UIView height, and also the beginning of my following section is blank until there is a reloadData call on it.
Here is my code so far:
WhiteBackgroundCollectionReusableView Class
:
#import <UIKit/UIKit.h>
@interface WhiteBackgroundCollectionReusableView : UICollectionReusableView
@end
@implementation WhiteBackgroundCollectionReusableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor redColor]];
}
return self;
}
@end
WhiteBackgroundCollectionViewFlowLayout Class
:
#import <UIKit/UIKit.h>
#import "WhiteBackgroundCollectionReusableView.h"
@interface WhiteBackgroundCollectionViewFlowLayout : UICollectionViewFlowLayout
@end
@implementation WhiteBackgroundCollectionViewFlowLayout
- (id)init
{
self = [super init];
if (self) {
// Initialization code
[self registerClass:[WhiteBackgroundCollectionReusableView class] forDecorationViewOfKind:@"WhiteSection"];
}
return self;
}
- (UICollectionViewLayoutAttributes*)layoutAttributesForDecorationViewOfKind:(NSString*)decorationViewKind atIndexPath:(NSIndexPath*)indexPath
{
UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
if(indexPath.section == 0) {
layoutAttributes.frame = CGRectMake(0.0, 0.0, self.collectionViewContentSize.width, 100);
layoutAttributes.zIndex = -1;
}
return layoutAttributes;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *allAttributes = [[NSMutableArray alloc] initWithCapacity:4];
[allAttributes addObject:[self layoutAttributesForDecorationViewOfKind:@"WhiteSection" atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]];
for(NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
[allAttributes addObject:layoutAttributes];
}
return allAttributes;
}
@end
I then assign my flow layout to my UICollectionView in my UICollectionViewController
in viewDidLoad
:
[_collectionView setCollectionViewLayout:[[WhiteBackgroundCollectionViewFlowLayout alloc] init]];
Any idea where I can go from here??
UPDATE:
I've fixed the issue where the next section was not showing. To do this I needed to change the zIndex to 1, so my layoutAttributesForElementsInRect
function now looks like this:
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *array = [super layoutAttributesForElementsInRect:rect];
for(UICollectionViewLayoutAttributes *attributes in array) {
attributes.zIndex = 1;
}
NSMutableArray *newArray = [array mutableCopy];
[newArray addObject:[self layoutAttributesForDecorationViewOfKind:@"WhiteSection" atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]];
for(NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
[newArray addObject:layoutAttributes];
}
array = [NSArray arrayWithArray:newArray];
return array;
}
So for now all I need is a way of getting the height of a section! PLEASE HELP! :)