封装了一个支持多个分区的iOS自适应动态宽度layout

支持多分区的动态自适应宽度layout,完善了之前只支持一个分区的布局,这里直接上代码,可以用来在商品sku, 搜索记录,编辑tab等场景的使用,灵活性强,支持代理配置

复制代码
//
//  LBNumberCenterEditTabLayout.m

//
//  Created by liubo on 26.3.25.

//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol LBNumberCenterEditTabLayout.mDelegate <NSObject>

- (CGSize)sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

- (CGSize)sizeForHeaderViewAtSection:(NSInteger)section;

@end

@interface LBNumberCenterEditTabLayout.m : UICollectionViewFlowLayout

@property (nonatomic, weak) id <LIVNumberCenterEditTabLayoutDelegate> delegate;

@property (nonatomic, assign) CGFloat contentWidth;

@end

NS_ASSUME_NONNULL_END


//
//  LBNumberCenterEditTabLayout.m
//  
//
//  Created by liubo on 26.3.25.
//

#import "LBNumberCenterEditTabLayout.m"

@interface LBNumberCenterEditTabLayout.m ()

@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *layoutAttributesArray;

@property (nonatomic, assign) CGSize contentSize;

@end

@implementation LBNumberCenterEditTabLayout.m

- (instancetype)init {
    if (self = [super init]) {
        self.layoutAttributesArray = [NSMutableArray new];
    }
    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];
    CGFloat currentY = 0;
    CGFloat x = self.sectionInset.left;
    // 计算新的布局
    NSInteger sectionCount = [self.collectionView numberOfSections];
    for (int i = 0; i < sectionCount; i ++) {
        CGSize headerSize = CGSizeZero;
        if (self.delegate && [self.delegate respondsToSelector:@selector(sizeForHeaderViewAtSection:)]) {
            headerSize = [self.delegate sizeForHeaderViewAtSection:i];
        }
        if (!CGSizeEqualToSize(headerSize, CGSizeZero)) {
            UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:i]];;
            layoutAttributes.frame = CGRectMake(0, currentY, headerSize.width, headerSize.height);
            currentY += headerSize.height;
            [self.layoutAttributesArray addObject:layoutAttributes];
        }
        NSInteger count = 0;
        if ([self.collectionView.dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
            count = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:0];
        }
        CGFloat currentX = x;

        if (count > 0) {
            for (int j = 0; j < count; j++) {
                CGSize cellSize = [self sizeForItemAtIndexPath:[NSIndexPath indexPathForItem:j inSection:i]];
                CGFloat cellWidth = cellSize.width;
                CGFloat cellHeight = cellSize.height;
                // 创建布局属性
                UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:j inSection:i]];
                layoutAttributes.frame = CGRectMake(currentX, currentY, cellWidth, cellHeight);
                [self.layoutAttributesArray addObject:layoutAttributes];
                currentX += (self.minimumInteritemSpacing + cellWidth);
                // 计算下一个item的x,以及布局更新结束检测
                if (j != count - 1) {
                    if (currentX + cellWidth + self.minimumInteritemSpacing + self.sectionInset.right > self.contentWidth) {
                        currentX = self.sectionInset.left;
                        currentY += self.minimumLineSpacing + cellHeight;
                    }
                } else {
                    currentY += cellHeight;
                }
                if (i == sectionCount - 1 && j == count - 1) {
                    self.contentSize = CGSizeMake(self.contentWidth, currentY);
                }
            }
        }
    }
}

- (CGSize)sizeForItemAtIndexPath:(NSIndexPath *)index {
    CGSize size = CGSizeZero;
    if ([self.delegate respondsToSelector:@selector(sizeForItemAtIndexPath:)]) {
        size = [self.delegate sizeForItemAtIndexPath:index];
    }
    return size;
}

@end
相关推荐
lilili啊啊啊6 小时前
IOS奔溃日志分析-克魔ios开发助手实战-以支付宝奔溃日志为例
ios
风吹草地现牛羊的马7 小时前
mac m1/m2/m3 pyaudio的安装
深度学习·macos·自然语言处理·#pyaudio
文公子WGZ7 小时前
Razer macOS v0.4.10快速安装
macos
lichao8904278 小时前
JBDev - Theos下一代越狱开发工具
ios
二流小码农11 小时前
鸿蒙开发:使用Ellipse绘制椭圆
android·ios·harmonyos
二流小码农12 小时前
鸿蒙开发:使用Circle绘制圆形
android·ios·harmonyos
Macle_Chen12 小时前
XCode中使用MonkeyDev开发iOS版的Comand-line Tool的daemon程序
macos·ios·xcode·逆向·comand-line
二流小码农13 小时前
鸿蒙开发:使用Rect绘制矩形
android·ios·harmonyos
丁乾坤的博客15 小时前
iOS审核被拒:Missing privacy manifest 第三方库添加隐私声明文件
ios·manifest·第三方库隐私文件
Unlimitedz17 小时前
iOS GCD
macos·ios·cocoa