iOS UICollectionView 均分间隙处理

1.当我们在开发过程中会屏幕均分成几个Item,在部分屏幕中会存在间隙问题,为了解决这个问题,直接上代码,新建FYUserCenterFlowLayout 继承 UICollectionViewFlowLayout工具类:

.h

NS_ASSUME_NONNULL_BEGIN

@interface FYUserCenterFlowLayout : UICollectionViewFlowLayout

@end

NS_ASSUME_NONNULL_END

.m

#import "FYUserCenterFlowLayout.h"

@implementation FYUserCenterFlowLayout

-(NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{

    NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
    for(int i = 1; i < [answer count]; ++i) {
        UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
        UICollectionViewLayoutAttributes*prevLayoutAttributes=answer[i-1];
        NSInteger maximumSpacing = 0;
        NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
        if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self. collectionViewContentSize.width) {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + maximumSpacing;
            currentLayoutAttributes.frame = frame;
        }
    }
    return answer;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。