Files
keyboard/CustomKeyboard/View/KBKeyboardView/KBKeyboardRowContainerBuilder.m
2026-03-04 12:54:57 +08:00

60 lines
1.8 KiB
Objective-C

//
// KBKeyboardRowContainerBuilder.m
// CustomKeyboard
//
#import "KBKeyboardRowContainerBuilder.h"
#import <Masonry/Masonry.h>
@implementation KBKeyboardRowContainerBuilder
- (void)rebuildRowContainersForRows:(NSArray<KBKeyboardRowConfig *> *)rowConfigs
inContainer:(UIView *)container
rowViews:(NSMutableArray<UIView *> *)rowViews
rowSpacing:(CGFloat)rowSpacing
topInset:(CGFloat)topInset
bottomInset:(CGFloat)bottomInset {
if (!container || !rowViews) { return; }
for (UIView *row in rowViews) {
[row removeFromSuperview];
}
[rowViews removeAllObjects];
NSUInteger rowCount = rowConfigs.count;
if (rowCount == 0) { return; }
UIView *firstRow = nil;
UIView *previousRow = nil;
for (NSUInteger i = 0; i < rowCount; i++) {
UIView *rowView = [UIView new];
[container addSubview:rowView];
[rowViews addObject:rowView];
[rowView mas_makeConstraints:^(MASConstraintMaker *make) {
if (previousRow) {
make.top.equalTo(previousRow.mas_bottom).offset(rowSpacing);
} else {
make.top.equalTo(container.mas_top).offset(topInset);
}
make.left.right.equalTo(container);
// 所有行等高,自动根据可用空间分配行高
if (firstRow) {
make.height.equalTo(firstRow);
}
}];
// 最后一行锚定到底部
if (i == rowCount - 1) {
[rowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(container.mas_bottom).offset(-bottomInset);
}];
}
if (!firstRow) { firstRow = rowView; }
previousRow = rowView;
}
}
@end