答案 0 :(得分:2)
您应该使用嵌套的StackView
。首先在Horizontal StackView
中嵌入View1和View2。按比例填充对齐属性中心和分布。然后将Horizontal StackView
嵌入Vertical Stackview
。在这里,我附上了我的演示屏幕截图:
答案 1 :(得分:1)
堆栈视图使用“自动布局”来定位和调整其排列视图的大小。堆栈视图将第一个和最后一个排列的视图与其沿着堆栈轴的边缘对齐。的
In a horizontal stack, this means the first arranged view’s leading edge is pinned to the stack’s leading edge, and the last arranged view’s trailing edge is pinned to the stack’s trailing edge.
强>
您可以改为使用约束。
答案 2 :(得分:1)
我可以在添加视图后使UIStackView自动增长宽度
代码
class StackSampleViewController:UIViewController {
@IBOutlet weak var stackView: UIStackView!
//Keep center auto grow with subviews
@IBAction func touchUpAdd(_ sender: Any) {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
if (stackView.subviews.count % 2) == 0 {
view.widthAnchor.constraint(equalToConstant: 100).isActive = true
view.backgroundColor = .black
} else {
view.widthAnchor.constraint(equalToConstant: 50).isActive = true
view.backgroundColor = .red
}
stackView.addArrangedSubview(view)
}
}
答案 3 :(得分:0)