StackView 中的按钮不保持大小限制

Buttons not maintaining size constraints in StackView

我正在编写实施自定义控件 iOS 教程(我会 link 但不允许使用超过两个 link)。

我现在在 StackView 中生成了 5 个按钮。我为每个按钮实现了这些约束:

button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true

我完全按照指定输入了所有代码,但是当我 运行 应用程序时,按钮会垂直和水平填充堆栈视图:

如果我将 StackView 的对齐方式 属性 设置为顶部,我可以防止按钮垂直拉伸,但最后一个按钮仍然水平拉伸:

我尝试了很多不同的方法来防止最后一个按钮被拉伸,但都无济于事。在控制台中,我看到大小限制被覆盖了,但我不知道是什么。

我希望这些按钮在 StackView 中保持指定的高度和宽度 (44)。关于如何做到这一点的任何想法?如果需要,我可以提供更多信息。

试试这个,它工作正常。

-(void)makeFiveButton {

UIButton *button1 = [[UIButton alloc]init];
button1.backgroundColor = [UIColor greenColor];
UIButton *button2 = [[UIButton alloc]init];
button2.backgroundColor = [UIColor redColor];
UIButton *button3 = [[UIButton alloc]init];
button3.backgroundColor = [UIColor yellowColor];
UIButton *button4 = [[UIButton alloc]init];
button4.backgroundColor = [UIColor purpleColor];
UIButton *button5 = [[UIButton alloc]init];
button5.backgroundColor = [UIColor brownColor];



UIStackView *stackView = [[UIStackView alloc]init];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionFill;
stackView.alignment = UIStackViewAlignmentFill;
stackView.spacing = 10;


[stackView addArrangedSubview:button1];
[stackView addArrangedSubview:button2];
[stackView addArrangedSubview:button3];
[stackView addArrangedSubview:button4];
[stackView addArrangedSubview:button5];
[self.view addSubview:stackView];

button1.translatesAutoresizingMaskIntoConstraints = NO;
button2.translatesAutoresizingMaskIntoConstraints = NO;
button3.translatesAutoresizingMaskIntoConstraints = NO;
button4.translatesAutoresizingMaskIntoConstraints = NO;
button5.translatesAutoresizingMaskIntoConstraints = NO;
stackView.translatesAutoresizingMaskIntoConstraints = NO;


[button1.heightAnchor constraintEqualToConstant:44].active = true;
[button1.widthAnchor constraintEqualToConstant:44].active = true;

[button2.heightAnchor constraintEqualToConstant:44].active = true;
[button2.widthAnchor constraintEqualToConstant:44].active = true;

[button3.heightAnchor constraintEqualToConstant:44].active = true;
[button3.widthAnchor constraintEqualToConstant:44].active = true;

[button4.heightAnchor constraintEqualToConstant:44].active = true;
[button4.widthAnchor constraintEqualToConstant:44].active = true;

[button5.heightAnchor constraintEqualToConstant:44].active = true;
[button5.widthAnchor constraintEqualToConstant:44].active = true;

[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = true;
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = true;

}

如果您按照手册操作,一切都会在稍后运行

我遇到了类似的问题,但所选答案对我不起作用。我能够通过将对齐设置为前导并将分布设置为 equalSpacing 来修复它:

private func setupButtons() {
    // My Fix
    self.alignment = .leading
    self.distribution = .equalSpacing

    for _ in 0..<5 {
        let button = UIButton()
        button.backgroundColor = UIColor.red
        button.translatesAutoresizingMaskIntoConstraints = false
        button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
        button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
        button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)
        addArrangedSubview(button)
    }
}

或通过 IB: