Created
March 17, 2023 02:42
-
-
Save aiqinxuancai/783705d721e71a66e47c856ae402c1f1 to your computer and use it in GitHub Desktop.
Masonry的流式布局
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 实现流式按钮布局,自定义View如果支持sizeToFit,则也可以使用 | |
/// @param superView 按钮将要放在的容器 | |
/// @param buttons 按钮数组 | |
/// @param horSpacing 横向间隔 | |
/// @param verSpacing 行间隔 | |
/// @param lineHeight 行高 其实可以按照sizeToFit后的数据 | |
/// @param superViewWidth 这个是唯一的需要提前计算的点,不过也不会太复杂 | |
- (void)buttonFlowView:(UIView *)superView | |
buttons:(NSArray<UIButton *> *)buttons | |
horSpacing:(CGFloat)horSpacing | |
verSpacing:(CGFloat)verSpacing | |
lineHeight:(CGFloat)lineHeight | |
superViewWidth:(CGFloat)superViewWidth { | |
// 定义一个变量来跟踪当前行中已经添加了多少个按钮 | |
CGFloat currentRowWidth = 0; | |
[superView removeAllSubViews]; | |
//计算最小的button尺寸 | |
for (UIButton *button in buttons) { | |
[superView addSubview:button]; | |
[button sizeToFit]; | |
} | |
CGFloat currentRow = 0; | |
NSMutableArray<NSMutableArray<UIButton *> *> *buttonsSubArray = [NSMutableArray<NSMutableArray<UIButton *> *> new]; | |
NSMutableArray<UIButton *> *lineArray = [NSMutableArray<UIButton *> new]; | |
for (UIButton *button in buttons) { | |
CGFloat buttonWidth = button.frame.size.width; | |
// 如果加上了这个按钮 已经不够空间了 则换行 | |
if (currentRowWidth + horSpacing + buttonWidth > superViewWidth) { // 重置计数器 换行 | |
currentRowWidth = 0; | |
currentRow++; | |
[buttonsSubArray addObject:lineArray]; | |
lineArray = [NSMutableArray<UIButton *> new]; | |
[lineArray addObject:button]; | |
} else { | |
[lineArray addObject:button]; | |
} | |
if (buttons.lastObject == button) { | |
[buttonsSubArray addObject:lineArray]; | |
} | |
// 更新计数器 | |
currentRowWidth += horSpacing + buttonWidth; | |
} | |
currentRow = 0; | |
CGFloat fullHeight = 0; | |
for (NSMutableArray<UIButton *> *buttonLine in buttonsSubArray) { | |
if (buttonLine.count < 2) { | |
[buttonLine mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.top.mas_equalTo(superView).offset(currentRow * lineHeight + currentRow * verSpacing); | |
make.height.offset(lineHeight); | |
make.left.mas_equalTo(superView); | |
make.right.mas_equalTo(superView); | |
}]; | |
} else { | |
[buttonLine mas_distributeViewsAlongAxis:MASAxisTypeHorizontal | |
withFixedSpacing:horSpacing //横向间隔 | |
leadSpacing:0 | |
tailSpacing:0]; | |
[buttonLine mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.top.mas_equalTo(superView).offset(currentRow * lineHeight + currentRow * verSpacing); | |
make.height.offset(lineHeight); | |
//make.left.mas_equalTo(superView); | |
//make.right.mas_equalTo(superView); | |
}]; | |
} | |
fullHeight += verSpacing + lineHeight; | |
currentRow++; | |
if (buttonsSubArray.lastObject == buttonLine) { | |
[superView mas_updateConstraints:^(MASConstraintMaker *make) { | |
make.bottom.mas_equalTo(buttonsSubArray.lastObject); | |
}]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment