嵌套的 UIStackViews 在编程中不工作 UI
Nested UIStackViews not working in programmatic UI
我在一个有 4 个水平子视图(行)的 mainStackView (UIStackView) 中工作,它工作正常如下(mainStackView 使用 AutoLayout 约束进行管理):
rowsArray = Array<UIView>()
for _ in 0...3 {
let row = UIView()
row.backgroundColor = UIColor.randomColor() //color for debugging
rowsArray.append(row)
}
mainStackView = UIStackView(arrangedSubviews: rowsArray)
mainStackView.axis = .vertical
mainStackView.alignment = .fill
mainStackView.distribution = .fillEqually
mainStackView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(mainStackView)
现在,鉴于每个子视图(在本例中为行)中将包含多个水平子视图,我需要将 arrangedSubviews 设置为数组,但它无法显示任何子视图。
rowsArray = Array<UIStackView>()
for _ in 0...3 {
let row = UIStackView()
row.backgroundColor = UIColor.randomColor() //color for debugging
rowsArray.append(row)
}
知道为什么 UIStackView 不像超类 (UIView) 那样工作,或者是否有任何其他考虑以编程方式嵌套 UIStackViews
Rgds...e
这不是错误,它就是这样设计的,请阅读 docs 部分 管理堆栈视图的外观 的以下摘录:
The UIStackView is a nonrendering subclass of UIView; that is, it does not provide any user interface of its own. Instead, it just manages the position and size of its arranged views. As a result, some properties (like backgroundColor) have no effect on the stack view.
这意味着 UIStackView
不是经典的 UIView
,而是一个很好的布局其子视图的界面。创建一个没有排列的子视图(这是你的情况)的 UIStackView
是没有意义的。
将子视图添加到您创建的那些行,将它们的背景颜色设置为红色,您将看到:
rowsArray = Array<UIStackView>()
for _ in 0...3 {
let row = UIStackView()
let view = UIView()
view.backgroundColor = UIColor.randomColor() //color for debugging
row.addArrangedSubview(view)
rowsArray.append(row)
}
关于嵌套的 stackViews,我已经使用了很长一段时间没有出现问题,所以我相信这是一个可行的方法。
我在一个有 4 个水平子视图(行)的 mainStackView (UIStackView) 中工作,它工作正常如下(mainStackView 使用 AutoLayout 约束进行管理):
rowsArray = Array<UIView>()
for _ in 0...3 {
let row = UIView()
row.backgroundColor = UIColor.randomColor() //color for debugging
rowsArray.append(row)
}
mainStackView = UIStackView(arrangedSubviews: rowsArray)
mainStackView.axis = .vertical
mainStackView.alignment = .fill
mainStackView.distribution = .fillEqually
mainStackView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(mainStackView)
现在,鉴于每个子视图(在本例中为行)中将包含多个水平子视图,我需要将 arrangedSubviews 设置为数组,但它无法显示任何子视图。
rowsArray = Array<UIStackView>()
for _ in 0...3 {
let row = UIStackView()
row.backgroundColor = UIColor.randomColor() //color for debugging
rowsArray.append(row)
}
知道为什么 UIStackView 不像超类 (UIView) 那样工作,或者是否有任何其他考虑以编程方式嵌套 UIStackViews
Rgds...e
这不是错误,它就是这样设计的,请阅读 docs 部分 管理堆栈视图的外观 的以下摘录:
The UIStackView is a nonrendering subclass of UIView; that is, it does not provide any user interface of its own. Instead, it just manages the position and size of its arranged views. As a result, some properties (like backgroundColor) have no effect on the stack view.
这意味着 UIStackView
不是经典的 UIView
,而是一个很好的布局其子视图的界面。创建一个没有排列的子视图(这是你的情况)的 UIStackView
是没有意义的。
将子视图添加到您创建的那些行,将它们的背景颜色设置为红色,您将看到:
rowsArray = Array<UIStackView>()
for _ in 0...3 {
let row = UIStackView()
let view = UIView()
view.backgroundColor = UIColor.randomColor() //color for debugging
row.addArrangedSubview(view)
rowsArray.append(row)
}
关于嵌套的 stackViews,我已经使用了很长一段时间没有出现问题,所以我相信这是一个可行的方法。