Is there any way to customize UICollectionViewFlowLayout
, without UICollectionViewDelegateFlowLayout methods
?
And could parse the UICollectionViewLayoutAttributes
with size
given, without giving origin
?
So I need not implement the frame of per UICollectionViewLayoutAttributes
in the - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
method.
Not calculating the origin of each Item does a lot convenience.
Here is my Sample code:
// call the two layout calculating methods , and combine.
- (void)prepareLayout{
[super prepareLayout];
if(self.attributesArray.count>0){
[self.attributesArray removeAllObjects];
}
NSInteger indexCount = [self.collectionView numberOfItemsInSection: 0];
for (NSInteger i = 0; i<indexCount; i++) {
NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection: 0];
UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath: indexPath];
[self.attributesArray addObject: attributes];
}
UICollectionViewLayoutAttributes * headAttributes = [self layoutAttributesForSupplementaryViewOfKind: UICollectionElementKindSectionHeader atIndexPath: [NSIndexPath indexPathForItem:0 inSection: 0]];
[self.attributesArray addObject: headAttributes];
}
// calculate the items layout
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewLayoutAttributes * cellAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath: indexPath];
CGFloat baseOriginY = 133;
switch (indexPath.item) {
case 0:
{
cellAttributes.frame = CGRectMake(0, baseOriginY, KScreenWidth, 50);
}
break;
case 6: //7:
case 7: //8:
{
NSInteger count = indexPath.item-6;
cellAttributes.frame = CGRectMake(0, 155 + 50*count + baseOriginY, KScreenWidth, 50);
}
break;
case 5: //6:
{
cellAttributes.frame = CGRectMake(0, 145 + baseOriginY, KScreenWidth, 10);
}
break;
default:
{
NSInteger i = indexPath.item - 1;
cellAttributes.frame = CGRectMake(i*KScreenWidth/4.0, 50 + baseOriginY, KScreenWidth/4.0, 95);
}
break;
}
return cellAttributes;
}
// calculate the headers layout
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
UICollectionViewLayoutAttributes * headerAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind: UICollectionElementKindSectionHeader withIndexPath: indexPath];
// headerAttributes.size = CGSizeMake(KScreenWidth, 133);
headerAttributes.frame = CGRectMake(0, 0, KScreenWidth, 133);
return headerAttributes;
}
// return the final layout results
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
return self.attributesArray;
}
In other words, use the adjusts dynamically feature of UICollectionViewFlowLayout
, without conforms to UICollectionViewDelegateFlowLayout
protocol.
The adjusts dynamically feature, as Apple says:
Flow layouts lay out their content using a fixed distance in one direction and a scrollable distance in the other. For example, in a vertically scrolling grid, the width of the grid content is constrained to the width of the corresponding collection view while the height of the content adjusts dynamically to match the number of sections and items in the grid.
I think it is quite possible, I don't know how to implement it now. Maybe a Apple's API.
Any advice or explanation is very welcomed.
Many Thanks in advance.