gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:18:59 26 4
gpt4 key购买 nike

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

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

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

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

enter image description here

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

enter image description here

我尝试了许多不同的方法来防止最后一个按钮被拉伸(stretch),但没有效果。在控制台中,我看到大小限制被覆盖,但我无法弄清楚是什么。

我希望这些按钮在 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;

}

关于ios - StackView 中的按钮不保持大小限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com