I have a UICollectionView with a gray background, however I want one of my sections to have a white background. I found that it was not as easy as I had hoped (i.e. a section having an attribute that I could just set).
From a glance at How to change the background color for an entire section in a UICollectionView? It was suggested to use the decor view and this question UICollectionView Decoration View helped me change the color of the section by subclassing UICollectionReusableViewand UICollectionViewFlowLayout.
My problem is that I cannot get the height of the section (which is dynamic) to set the height of the UIView, and also the beginning of my next section is empty until the reloadData request appears on it.
Here is my code
WhiteBackgroundCollectionReusableView Class::
#import <UIKit/UIKit.h>
@interface WhiteBackgroundCollectionReusableView : UICollectionReusableView
@end
@implementation WhiteBackgroundCollectionReusableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[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) {
[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
Then I assign my stream layout to my UICollectionView in mine UICollectionViewControllerin viewDidLoad:
[_collectionView setCollectionViewLayout:[[WhiteBackgroundCollectionViewFlowLayout alloc] init]];
Any idea that I can move from here
UPDATE:
I fixed the problem when the next section was not showing. To do this, I needed to change zIndex to 1, so my function layoutAttributesForElementsInRectnow 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 now all I need is a way to get the section height! PLEASE HELP! :)