封装了一个iOS水平方向动态宽度layout

ops/2024/10/22 16:21:06/

我们有时候会遇到这样的情形,就是需要展示一些动态的标签,宽度是动态的,
水平方向是一行,其实这种情况还是比较容易处理的,只是一下子想不起来,
这里做了一个相关的需求,将思路和代码记录下来,方便后续的查找
效果图
请添加图片描述

思路:就是重写自定义layout的prepare layout 方法, 在里面计算每一个item 的frame
。通过[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]] 方法创建 UICollectionViewLayoutAttributes 对象,然后个UICollectionViewLayoutAttributes 对象的frame赋值,这里要记录上一个UICollectionViewLayoutAttributes对象的x.及总的宽度。通过这句代码赋值frame
layoutAttributes.frame = CGRectMake(currentX, currentY, cellWidth, cellHeight);
然后将布局属性添加到一个数组中
在layoutAttributesForElementsInRect:方法中返回数组。
在 collectionViewContentSize 方法中返回宽度
还有一点,我们需要通过代理方式获取到单个item的宽度

  • (CGFloat)cellWidthForItemAtIndex:(NSInteger)index;

  • (CGFloat)footerWidthForFooterAtSection:(NSInteger)section;

代码

@protocol LIUNumberCenterAssociationLayoutDelegate <NSObject>- (CGFloat)cellWidthForItemAtIndex:(NSInteger)index;- (CGFloat)footerWidthForFooterAtSection:(NSInteger)section;@end@interface LIUNumberCenterAssociationLayout : UICollectionViewFlowLayout@property (nonatomic, weak) id <LIUNumberCenterAssociationLayoutDelegate> delegate;@end
//
//  LIUNumberCenterAssociationLayout.m
//#import "LIUNumberCenterAssociationLayout.h"@interface LIUNumberCenterAssociationLayout ()@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *layoutAttributesArray;@property (nonatomic, assign) CGSize contentSize;@end@implementation LIUNumberCenterAssociationLayout- (instancetype)init {if (self = [super init]) {self.layoutAttributesArray = [NSMutableArray new];self.contentSize = CGSizeMake(16, 22); // 16是左边距}return self;
}- (void)prepareLayout {[super prepareLayout];[self updateLayout];
}- (CGSize)collectionViewContentSize {return self.contentSize;
}- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {return self.layoutAttributesArray;
}#pragma mark - ---------- update ----------- (void)updateLayout {// 移除旧的布局[self.layoutAttributesArray removeAllObjects];self.contentSize = CGSizeMake(8, 28); // 16是左边距// 计算新的布局NSInteger count = 0;if ([self.collectionView.dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {count = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:0];}CGFloat x = 0; if (count > 0) {for (int i = 0; i < count; i++) {CGFloat currentX = x;CGFloat currentY = 0;CGFloat cellWidth = [self cellWidthForItemAtIndex:i];CGFloat cellHeight = 28;// 创建布局属性UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];layoutAttributes.frame = CGRectMake(currentX, currentY, cellWidth, cellHeight);[self.layoutAttributesArray addObject:layoutAttributes];// 计算下一个item的x,以及布局更新结束检测if (i < count - 1) {x = currentX + cellWidth + 8; // 8是间距} else {x = currentX + cellWidth + 8; // 8是右边距self.contentSize = CGSizeMake(x, 0);}}}if ([self.delegate respondsToSelector:@selector(footerWidthForFooterAtSection:)]) {CGFloat footerWidth = [self.delegate footerWidthForFooterAtSection:0];UICollectionViewLayoutAttributes *layoutattribute = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];layoutattribute.frame = CGRectMake(x, 0, footerWidth, 28);x += footerWidth ;self.contentSize = CGSizeMake(x, 28);[self.layoutAttributesArray addObject:layoutattribute];}
}- (CGFloat)cellWidthForItemAtIndex:(NSInteger)index {CGFloat width = 0;if ([self.delegate respondsToSelector:@selector(cellWidthForItemAtIndex:)]) {width = [self.delegate cellWidthForItemAtIndex:index];}return width;
}@end

http://www.ppmy.cn/ops/121153.html

相关文章

蓝桥等级考试C++组17级真题-2023-05-21

单项选择题 **1、CL17(15分)**选择题 关于面向对象&#xff0c;以下说法正确的是( ) A. C语言是面向对象的语言 B. C语言只支持面向对象的程序设计 C. C语言是面向对象的语言&#xff0c;但C语言不是 D. C语言中的类和int、char等类型一样&#xff0c;都是基本数据类型 2、CL1…

安全服务面试

118.什么叫脱壳? 而从技术的角度出发&#xff0c;壳是一段执行于原始程序前的代码。原始程序的代码在加 壳的过程中可能被压缩、加密……。当加壳后的文件执行时&#xff0c;壳&#xff0d;这段代码先于 原始程序运行&#xff0c;他把压缩、加密后的代码还原成原始程序代码…

基于NFSR和S盒的国产流密码算法Bagua

基于NFSR和S盒的国产流密码算法Bagua 0x0 Bagua算法简介 流密码算法Bagua是面向5G数据通信、面向硬件设计的密码算法。它是由国内知名密码专家学者谭林、朱宣勇、戚文峰共同设计的密码算法,发表在2020年国际信息安全与密码会议上(Inscrypt 2020)。该算法是基于Galois结构的非…

python 实现linear algebra线性代数算法

linear algebra线性代数算法介绍 线性代数&#xff08;Linear Algebra&#xff09;是一个广泛的数学领域&#xff0c;涵盖了多个算法和概念&#xff0c;这些算法和概念在处理向量、矩阵、线性方程组、线性变换等方面发挥着重要作用。以下是一些线性代数中常见的算法和概念&…

Pikachu-Sql Inject-数字型注入(GET)

一、、破解 SQL 查询语句中的字段数 ?id1 order by 3 -- // -- 是注释&#xff0c; 加号 在MySQL中会转成空格 order by 1 &#xff0c;by 数字几&#xff0c;就是按照第几列进行排序&#xff1b;如果没有这一行&#xff0c;则报错 如&#xff1a;以下语句&#xff0c;根据…

HTTP【网络】

文章目录 HTTPURL(Uniform Resource Lacator) HTTP协议格式HTTP的方法HTTP的状态码HTTP常见的Header HTTP 超文本传输协议&#xff0c;是一个简单的请求-响应协议&#xff0c;HTTP通常运行在TCP之上 URL(Uniform Resource Lacator) 一资源定位符&#xff0c;也就是通常所说的…

VTK 与 OpenCV 的区别和各自的特点

VTK与OpenCV&#xff1a;各有所长&#xff0c;相辅相成 VTK和OpenCV都是强大的图像处理和计算机视觉工具&#xff0c;但它们在侧重点和功能上有着显著的区别。 1 VTK (Visualization Toolkit) 核心功能&#xff1a; VTK专注于三维可视化。它能够将复杂的数据&#xff08;如医…

【笔记】选择题笔记+数据结构笔记

文章目录 2014 41方法一先序遍历方法二 连通分量是极大连通子图 一个连通图的生成树是一个极小连通子图 无向图的邻接表中&#xff0c;第i个顶点的度为第i个链表中的结点数 邻接表和邻接矩阵对不同的操作各有优势。 最短路径算法: 单源最短路径 已知图G(V,E)&#xff0c;我们…