添加元素后 UIStackView 宽度和高度不变
UIStackView width & height not changing following elements addition
上下文
我正在尝试以编程方式创建 UIStackView,但它根本没有显示。
为了测试,我创建了 2 个小的 UIView
并将其添加到 UIStackView
。
我试过了
- 向
UIStackView
添加约束
- 使用
layoutIfNeeded
- 使用
layoutSubviews
但没有任何效果。
代码
import UIKit
class ViewController: UIViewController {
var contentStack = UIStackView()
@IBOutlet weak var mainScroll: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
contentStack.backgroundColor = #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 1)
contentStack.axis = .vertical
contentStack.spacing = 0
contentStack.alignment = .top
contentStack.distribution = .equalSpacing
let a_view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 30))
a_view.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
contentStack.addArrangedSubview(a_view)
let b_view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 40))
b_view.backgroundColor = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)
contentStack.addArrangedSubview(b_view)
mainScroll.addSubview(contentStack)
contentStack.topAnchor.constraint(equalTo: mainScroll.topAnchor).isActive = true
contentStack.leftAnchor.constraint(equalTo: mainScroll.leftAnchor).isActive = true
contentStack.bottomAnchor.constraint(equalTo: mainScroll.bottomAnchor).isActive = true
contentStack.rightAnchor.constraint(equalTo: mainScroll.rightAnchor).isActive = true
mainScroll.contentSize = CGSize(width: self.view.frame.width, height: contentStack.frame.height)
print("Stack Content Size: (\(contentStack.frame.width), \(contentStack.frame.height))");
}
}
输出
问题
阅读文档后,我假设UIStackView
的高度和宽度都适应了添加视图后堆栈视图中包含的元素的大小,即Stack Content Size: (0.0, 0.0)
?
尝试为 a_view 和 b_view 设置约束,如下所示
let a_view = UIView()
a_view.translatesAutoresizingMaskIntoConstraints = false
a_view.heightAnchor.constraint(equalToConstant: 30).isActive = true
a_view.widthAnchor.constraint(equalToConstant: 30).isActive = true
堆栈视图使用固有内容大小,因此使用布局约束来定义视图的尺寸。
如果这不起作用,那么我怀疑滚动视图约束可能存在一些问题。
上下文
我正在尝试以编程方式创建 UIStackView,但它根本没有显示。
为了测试,我创建了 2 个小的 UIView
并将其添加到 UIStackView
。
我试过了
- 向
UIStackView
添加约束
- 使用
layoutIfNeeded
- 使用
layoutSubviews
但没有任何效果。
代码
import UIKit
class ViewController: UIViewController {
var contentStack = UIStackView()
@IBOutlet weak var mainScroll: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
contentStack.backgroundColor = #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 1)
contentStack.axis = .vertical
contentStack.spacing = 0
contentStack.alignment = .top
contentStack.distribution = .equalSpacing
let a_view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 30))
a_view.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
contentStack.addArrangedSubview(a_view)
let b_view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 40))
b_view.backgroundColor = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)
contentStack.addArrangedSubview(b_view)
mainScroll.addSubview(contentStack)
contentStack.topAnchor.constraint(equalTo: mainScroll.topAnchor).isActive = true
contentStack.leftAnchor.constraint(equalTo: mainScroll.leftAnchor).isActive = true
contentStack.bottomAnchor.constraint(equalTo: mainScroll.bottomAnchor).isActive = true
contentStack.rightAnchor.constraint(equalTo: mainScroll.rightAnchor).isActive = true
mainScroll.contentSize = CGSize(width: self.view.frame.width, height: contentStack.frame.height)
print("Stack Content Size: (\(contentStack.frame.width), \(contentStack.frame.height))");
}
}
输出
问题
阅读文档后,我假设UIStackView
的高度和宽度都适应了添加视图后堆栈视图中包含的元素的大小,即Stack Content Size: (0.0, 0.0)
?
尝试为 a_view 和 b_view 设置约束,如下所示
let a_view = UIView()
a_view.translatesAutoresizingMaskIntoConstraints = false
a_view.heightAnchor.constraint(equalToConstant: 30).isActive = true
a_view.widthAnchor.constraint(equalToConstant: 30).isActive = true
堆栈视图使用固有内容大小,因此使用布局约束来定义视图的尺寸。
如果这不起作用,那么我怀疑滚动视图约束可能存在一些问题。