不在StackView中维护大小约束的按钮

问题描述 投票:2回答:3

我正在开发实现自定义控件iOS教程(我会链接它,但我不允许使用两个以上的链接)。

我正处于StackView中生成5个按钮的位置。我为每个按钮实现了这些约束:

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

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

enter image description here

如果我将StackView的alignment属性设置为Top,我可以阻止按钮垂直拉伸,但最后一个按钮仍然是水平拉伸的:

enter image description here

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

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

ios xcode uistackview
3个回答
2
投票

试试这个它工作正常。

-(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;

}

0
投票

我有一个类似的问题,但选定的答案对我不起作用。我能够通过将对齐设置为leading和distrbution为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:

enter image description here


-3
投票

如果你按照手册,一切都会工作

© www.soinside.com 2019 - 2024. All rights reserved.