// // KBKeyboardRowContainerBuilder.m // CustomKeyboard // #import "KBKeyboardRowContainerBuilder.h" #import @implementation KBKeyboardRowContainerBuilder - (void)rebuildRowContainersForRows:(NSArray *)rowConfigs inContainer:(UIView *)container rowViews:(NSMutableArray *)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