// // KBFunctionBarView.m // CustomKeyboard // // Created by Mac on 2025/10/28. // 功能 - barview #import "KBFunctionBarView.h" #import "Masonry.h" @interface KBFunctionBarView () @property (nonatomic, strong) UIView *leftContainer; // 左侧按钮容器 @property (nonatomic, strong) UIView *rightContainer; // 右侧按钮容器 @property (nonatomic, strong) NSArray *leftButtonsInternal; @property (nonatomic, strong) NSArray *rightButtonsInternal; @end @implementation KBFunctionBarView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; _leftTitles = @[@"帮回", @"会说", @"话术", @"更多"]; _rightTitles = @[@"❤", @"收藏", @"宫格"]; [self buildUI]; } return self; } #pragma mark - Public - (NSArray *)leftButtons { return self.leftButtonsInternal; } - (NSArray *)rightButtons { return self.rightButtonsInternal; } #pragma mark - UI - (void)buildUI { // 左右两个容器,方便分别布局 [self addSubview:self.leftContainer]; [self addSubview:self.rightContainer]; [self.rightContainer mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.mas_right).offset(-12); make.centerY.equalTo(self.mas_centerY); make.height.mas_equalTo(36); }]; [self.leftContainer mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.mas_left).offset(12); make.right.equalTo(self.rightContainer.mas_left).offset(-12); make.centerY.equalTo(self.mas_centerY); make.height.mas_equalTo(36); }]; // 左侧4个等宽按钮 NSMutableArray *leftBtns = [NSMutableArray arrayWithCapacity:4]; UIView *prev = nil; for (NSInteger i = 0; i < 4; i++) { UIButton *btn = [self buildButtonWithTitle:(i < self.leftTitles.count ? self.leftTitles[i] : [NSString stringWithFormat:@"L%ld", (long)i])]; btn.tag = 100 + i; [self.leftContainer addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { if (prev) { make.left.equalTo(prev.mas_right).offset(8); make.width.equalTo(prev); } else { make.left.equalTo(self.leftContainer.mas_left); } make.top.bottom.equalTo(self.leftContainer); }]; prev = btn; [leftBtns addObject:btn]; } [prev mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.leftContainer.mas_right); }]; self.leftButtonsInternal = leftBtns.copy; // 右侧3个等宽按钮(靠右) NSMutableArray *rightBtns = [NSMutableArray arrayWithCapacity:3]; UIView *next = nil; for (NSInteger i = 0; i < 3; i++) { UIButton *btn = [self buildButtonWithTitle:(i < self.rightTitles.count ? self.rightTitles[i] : [NSString stringWithFormat:@"R%ld", (long)i])]; btn.tag = 200 + i; [self.rightContainer addSubview:btn]; [rightBtns addObject:btn]; } // 从右往左排,保证整体靠右 UIButton *r0 = rightBtns.count > 0 ? rightBtns[0] : nil; UIButton *r1 = rightBtns.count > 1 ? rightBtns[1] : nil; UIButton *r2 = rightBtns.count > 2 ? rightBtns[2] : nil; if (r0 && r1 && r2) { [r2 mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.rightContainer.mas_right); make.top.bottom.equalTo(self.rightContainer); }]; [r1 mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(r2.mas_left).offset(-8); make.top.bottom.equalTo(self.rightContainer); make.width.equalTo(r2); }]; [r0 mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(r1.mas_left).offset(-8); make.top.bottom.equalTo(self.rightContainer); make.width.equalTo(r1); make.left.greaterThanOrEqualTo(self.rightContainer.mas_left); // 内容撑开 }]; } self.rightButtonsInternal = rightBtns.copy; } - (UIButton *)buildButtonWithTitle:(NSString *)title { UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.layer.cornerRadius = 18; btn.layer.masksToBounds = YES; btn.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9]; btn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; [btn setTitle:title forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; return btn; } #pragma mark - Lazy - (UIView *)leftContainer { if (!_leftContainer) { _leftContainer = [[UIView alloc] init]; } return _leftContainer; } - (UIView *)rightContainer { if (!_rightContainer) { _rightContainer = [[UIView alloc] init]; } return _rightContainer; } @end