本文共 5141 字,大约阅读时间需要 17 分钟。
<div#endif><div#endif><h2[sizeofContext
<divsqlite Milano
<div
通过自定义 UICollectionView FlowLayout,我们可以为集合视图的每个组增加背景视图,设置不同的样式属性。以下将详细介绍自定义 FlowLayout 的实现步骤。
在实现自定义 FlowLayout 之前,我们需要定义两个类和一个协议:
初始化 JHCollectionViewFlowLayout 类时,注册自定义的 UICollectionReusableView 类作为组背景的装饰视图:
NSString *const JHCollectionViewSectionBackground = @"JHCollectionViewSectionBackground";@interface JHCollectionViewFlowLayout : UICollectionViewFlowLayout@property (nonatomic, strong) NSMutableArray *decorationViewAttrs;@end@implementation JHCollectionViewFlowLayout- (instancetype)init { self = [super init]; if (self) { self.decorationViewAttrs = [NSMutableArray array]; [self setup]; } return self;}- (void)setup { [self registerClass:[JHCollectionReusableView class] forDecorationViewOfKind:JHCollectionViewSectionBackground];}
在 prepareLayout
方法中,通过调用委托方法获取每个组的背景颜色,并为每个组创建对应的布局属性。
[super prepareLayout];[self.decorationViewAttrs removeAllObjects];NSInteger numberOfSections = [self.collectionView numberOfSections];id денotes = self.collectionView.delegate;if (!numberOfSections || ![dentotes conformsToProtocol:@protocol(JHCollectionViewDelegateFlowLayout))] { return;}for (NSInteger section = 0; section < numberOfSections; section++) { NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section]; if (numberOfItems <= 0) { continue; } // 获取第一个和最后一个项目的布局属性 UICollectionViewLayoutAttributes *firstItemAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]]; UICollectionViewLayoutAttributes *lastItemAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:numberOfItems - 1 inSection:section]]; if (!firstItemAttr || !lastItemAttr) { continue; } // 获取组边距 UIEdgeInsets sectionInset = [self sectionInset]; if ([dentotes respondsToSelector:@selector(collectionView:layout:inlineInsetForSectionAtIndex:)]) { sectionInset = [dentotes collectionView:self.collectionView layout:self insetForSectionAtIndex:section]; } // 计算组的布局尺寸 CGRect sectionFrame = CGRectUnion(firstItemAttr.frame, lastItemAttr.frame); if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) { sectionFrame.size.width += sectionInset.left + sectionInset.right; sectionFrame.size.height = self.collectionView.frame.size.height; } else { sectionFrame.size.height += sectionInset.top + sectionInset.bottom; sectionFrame.size.width = self.collectionView.frame.size.width; } // 创建背景视图布局属性 JHCollectionViewLayoutAttributes *attr = [JHCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:JHCollectionViewSectionBackground withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]]; attr.frame = sectionFrame; attr.zIndex = -1; attr.backgroundColor = [dentotes collectionView:self.collectionView layout:self backgroundColorForSection:section]; [self.decorationViewAttrs addObject:attr];}
自定义的装饰视图类,用于显示组背景:
@interface JHCollectionReusableView : UICollectionReusableView@end@implementation JHCollectionReusableView- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes { [super applyLayoutAttributes:layoutAttributes]; if ([layoutAttributes isKindOfClass:[JHCollectionViewLayoutAttributes class]]) { JHCollectionViewLayoutAttributes *attr = (JHCollectionViewLayoutAttributes *)layoutAttributes; self.backgroundColor = attr.backgroundColor; }}
为了确保自定义布局能够正确显示,需要重写以下方法:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray *attrs = [[super layoutAttributesForElementsInRect:rect] mutableCopy]; for (UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs) { if (CGRectIntersectsRect(rect, attr.frame)) { [attrs addObject:attr]; } } return attrs;}- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath { if ([elementKind isEqualToString:JHCollectionViewSectionBackground]) { return [self.decorationViewAttrs objectAtIndex:indexPath.section]; } return [super layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:indexPath];}
在.storyboard 中定义自定义单元格,然后设置集合视图的 FlowLayout 参数:
如何设置不同的组背景颜色?
JHCollectionViewDelegateFlowLayout
协议中的 backgroundColorForSection
方法,传递每个组的背景颜色。如何显示组背景图像?
backgroundColor
。如何添加复杂的样式属性(如圆角)?
JHCollectionViewLayoutAttributes
中添加额外的属性(如 corner Foods
),并在自定义视图类中应用。通过以上步骤,您可以成功实现自定义可留意 FlorenceLayout,为集合视图的每个组设置独特的背景样式和布局属性。
转载地址:http://lovzk.baihongyu.com/