diff --git a/CustomKeyboard/View/KBFunctionBarView.m b/CustomKeyboard/View/KBFunctionBarView.m index 4bc6a89..cc8aca9 100644 --- a/CustomKeyboard/View/KBFunctionBarView.m +++ b/CustomKeyboard/View/KBFunctionBarView.m @@ -78,9 +78,8 @@ }]; self.leftButtonsInternal = leftBtns.copy; - // 右侧3个等宽按钮(靠右) + // 右侧N个按钮(靠右、两两等宽) NSMutableArray *rightBtns = [NSMutableArray arrayWithCapacity:3]; - UIView *next = nil; for (NSInteger i = 0; i < self.rightTitles.count; i++) { UIButton *btn = [self buildButtonWithTitle:(i < self.rightTitles.count ? self.rightTitles[i] : [NSString stringWithFormat:@"R%ld", (long)i])]; btn.tag = 200 + i; @@ -89,25 +88,27 @@ [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); + // 从右往左链式布局,保证整体靠右;支持 1/2/3... 任意数量 + UIView *prevRight = nil; // 指向右侧已布局的按钮 + for (NSInteger i = rightBtns.count - 1; i >= 0; i--) { + UIButton *btn = rightBtns[i]; + [btn mas_makeConstraints:^(MASConstraintMaker *make) { + if (!prevRight) { + // 最右侧按钮贴右 + make.right.equalTo(self.rightContainer.mas_right); + } else { + // 其余按钮紧挨左侧兄弟,且与其等宽 + make.right.equalTo(prevRight.mas_left).offset(-8); + make.width.equalTo(prevRight); + } 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); // 内容撑开 + prevRight = btn; + } + // 最左侧一个不超出容器左边(允许根据内容自然宽度收缩) + if (prevRight) { + [prevRight mas_makeConstraints:^(MASConstraintMaker *make) { + make.left.greaterThanOrEqualTo(self.rightContainer.mas_left); }]; }