gpt4 book ai didi

swift - UIStackView 不显示

转载 作者:可可西里 更新时间:2023-11-01 00:23:06 31 4
gpt4 key购买 nike

在我的 viewController viewDidLoad 方法中,我有:

    let stackView = UIStackView()
stackView.axis = .Vertical
self.view.addSubview(stackView)

for _ in 1..<100{
let vw = UIButton(type: .System)
vw.setTitle("Button", forState: .Normal)
stackView.addArrangedSubview(vw)
}

但是当我编译时,我只得到一个全白的屏幕。我做错了什么?

最佳答案

您正在将堆栈 View 添加到父 View ,但您没有告诉父 View 如何布置堆栈 View 。如果您使用基于约束的布局,则需要将堆栈 View 固定到父 View 的某些边缘:

view.addConstraint(NSLayoutConstraint(item: stackView, Attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1.0, constant: 0.0)
view.addConstraint(NSLayoutConstraint(item: stackView, Attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 0.0)
view.addConstraint(NSLayoutConstraint(item: stackView, Attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 1.0, constant: 0.0)

这会将堆栈 View 固定到 View 的顶部、前缘和后缘。

另一种方法是使用 Interface Builder 来设置约束,因为它们在代码中管理起来非常冗长。

关于swift - UIStackView 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35221390/

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