(Swift)设置UIcollectionView的section底色

作者 : 开心源码 本文共3784个字,预计阅读时间需要10分钟 发布时间: 2022-05-13 共271人阅读

前言

具体代码demo如下:

GitHub_OC版本:Demo具体代码

GitHub_Swift版本:Demo具体代码

码云_OC:Demo具体代码

??简单设计collectionview 底色和根据section不同设置不同颜色,支持collection横竖样式、自己设置偏移量、投影。
??因为APP设计样式的多样性,很多时候我们需要用到少量特别的样式,例如投影、圆角、某个空间下添加底色和投影等组合,这些看似很简单的样式,其实也需要花不少时间进行样式的布局和调整等。
??例如本人遇到需要在collectionView,根据section不同设置不同的底色,需要动态设置能否包含headerview,还需要设置投影等等,所以设计了这个可配置且动态升级的 collection 背景颜色 控件。可基本满足各种要求。

设计思路

1、继承UICollectionViewFlowLayout,重写prepareLayout方法,在方法内计算每个section的大小,并根据客户设置的sectiooninset,进行frame补充。
2、继承UICollectionViewLayoutAttributes,添加底色、投影等参数。
3、在prepareLayout计算每个section的UICollectionViewLayoutAttributes并设置底色参数,并进行保存,
4、在layoutAttributesForElementsInRect进行rect判断获取attr。
5、在applyLayoutAttributes内机进行样式设置。

效果图:

image

支持类型:

1、collectionView section底色。
2、能否包含headerview。
3、能否包含footerview。
4、支持borderWidth、borderColor。
5、支持shadow投影。
6、支持collectionView,Vertical,Horizontal。
7、支持根据不同section分别设置不同底色显示。

核心代码

/// 计算默认不包含headerview和footerview的背景大小/// @paramframeframe description/// @paramsectionInsetsectionInset description//MARK: 默认section无偏移大小extension JJCollectionViewRoundFlowLayout_Swift{    func calculateDefaultFrameWithSectionFrame(_ frame:CGRect ,sectionInset:UIEdgeInsets) -> CGRect{        var sectionFrame = frame;        sectionFrame.origin.x -= sectionInset.left;        sectionFrame.origin.y -= sectionInset.top;        if (self.scrollDirection == UICollectionView.ScrollDirection.horizontal) {            sectionFrame.size.width += sectionInset.left + sectionInset.right;            //减去系统adjustInset的top            if #available(iOS 11, *) {                sectionFrame.size.height = self.collectionView!.frame.size.height - self.collectionView!.adjustedContentInset.top;            } else {                sectionFrame.size.height = self.collectionView!.frame.size.height - abs(self.collectionView!.contentOffset.y)/*适配iOS11以下*/;            }        }else{            sectionFrame.size.width = self.collectionView!.frame.size.width;            sectionFrame.size.height += sectionInset.top + sectionInset.bottom;        }        return sectionFrame;    }}override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {    var attrs = super.layoutAttributesForElements(in: rect) ?? []    for attr in self.decorationViewAttrs {        attrs.append(attr)    }    return attrs}  override public func prepare() 代码有点多,就不贴出来了。下面有demo。

如何使用:

pod?’JJCollectionViewRoundFlowLayout_Swift’

//可选设置open var isCalculateHeader : Bool = false    // 能否计算headeropen var isCalculateFooter : Bool = false    // 能否计算footer
/// 设置底色偏移量(该设置只设置底色,与collectionview原sectioninsets区分)/// - Parameter collectionView: collectionView description/// - Parameter collectionViewLayout: collectionViewLayout description/// - Parameter section: section descriptionfunc collectionView(_ collectionView : UICollectionView , layout collectionViewLayout:UICollectionViewLayout,borderEdgeInsertsForSectionAtIndex section : Int) -> UIEdgeInsets;/// 设置底色相关/// - Parameter collectionView: collectionView description/// - Parameter collectionViewLayout: collectionViewLayout description/// - Parameter section: section descriptionfunc collectionView(_ collectionView : UICollectionView, layout collectionViewLayout : UICollectionViewLayout , configModelForSectionAtIndex section : Int ) -> JJCollectionViewRoundConfigModel_Swift;

在collectionview页面代码上加入代理商(JJCollectionViewDelegateRoundFlowLayout)

并实现如下两个方法:

#pragma mark - JJCollectionViewDelegateRoundFlowLayoutfunc collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, borderEdgeInsertsForSectionAtIndex section: Int) -> UIEdgeInsets {    return UIEdgeInsets.init(top: 5, left: 12, bottom: 5, right: 12)}func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, configModelForSectionAtIndex section: Int) -> JJCollectionViewRoundConfigModel_Swift {    let model = JJCollectionViewRoundConfigModel_Swift.init();        model.backgroundColor = UIColor.init(red: 233/255.0, green:233/255.0 ,blue:233/255.0,alpha:1.0)    model.cornerRadius = 10;    return model;}

效果如下:

image

具体代码demo如下:

GitHub_OC版本:Demo具体代码

GitHub_Swift版本:Demo具体代码

码云_OC:Demo具体代码 大家有空可star。

后续可能会单独升级swift版本,敬请期待。

如有问题,可以直接提issues,或者者发送邮件到kingjiajie@sina.com,或者者直接回复。谢谢。

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » (Swift)设置UIcollectionView的section底色

发表回复