博客
关于我
Section的背景色-UICollectionView
阅读量:760 次
发布时间:2019-03-23

本文共 5141 字,大约阅读时间需要 17 分钟。

<div#endif><div#endif><h2[sizeofContext

<divsqlite Milano

UICollectionView FlowLayout 自定义开发指南

<div

通过自定义 UICollectionView FlowLayout,我们可以为集合视图的每个组增加背景视图,设置不同的样式属性。以下将详细介绍自定义 FlowLayout 的实现步骤。

1. 类与协议定义

在实现自定义 FlowLayout 之前,我们需要定义两个类和一个协议:

  • JHCollectionViewFlowLayout:自定义的 FlowLayout 类,处理布局属性和背景设置。
  • JHCollectionViewDelegateFlowLayout:一个协议,定义委托方法,用于设置组背景颜色等属性。
  • JHCollectionReusableView:一个继承自 UICollectionReusableView 的自定义视图类,用于显示组背景。

2. 类实现细节

JHCollectionViewFlowLayout439>

初始化

初始化 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 方法中,通过调用委托方法获取每个组的背景颜色,并为每个组创建对应的布局属性。

prepareLayout499>

[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];}

JHCollectionReusableView

自定义的装饰视图类,用于显示组背景:

@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;    }}

FlowLayout 继续改进

为了确保自定义布局能够正确显示,需要重写以下方法:

- (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];}

3. 示例应用

在.storyboard 中定义自定义单元格,然后设置集合视图的 FlowLayout 参数:

  • isseurCell:设置为预定义的单元格类型。
  • rowCount:指定每个组的行数。
  • columnCount:指定每个组的列数。
  • sectionInset:设置组之间的边距。
  • insetForSectionAtIndex:通过委托方法设置不同组的内边距。

4. 常见问题

  • 如何设置不同的组背景颜色

    • 通过 JHCollectionViewDelegateFlowLayout 协议中的 backgroundColorForSection 方法,传递每个组的背景颜色。
  • 如何显示组背景图像

    • 将背景颜色替换为带有图像的颜色,并在自定义视图类中设置 backgroundColor
  • 如何添加复杂的样式属性(如圆角)?

    • JHCollectionViewLayoutAttributes 中添加额外的属性(如 corner Foods),并在自定义视图类中应用。

通过以上步骤,您可以成功实现自定义可留意 FlorenceLayout,为集合视图的每个组设置独特的背景样式和布局属性。

转载地址:http://lovzk.baihongyu.com/

你可能感兴趣的文章
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
mt-datetime-picker type="date" 时间格式 bug
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>