用虚线分隔线在三个子视图中划分堆栈视图?

时间:2017-11-23 13:58:14

标签: ios xcode uistackview

我试图将UIStackView分为三个单独的子视图,并用虚线划分它们。我知道您可以在UIStackView上设置间距,但据我所知,您无法将该间距更改为虚线。

基本上我希望我的三个子视图能够在不同的设备尺寸上正确缩放,但虚线在它们之间总是很小。为清楚起见,我试图实现的结果如下:

Example image of desired UIStackView layout

我希望有人能指出我正确的方向,先谢谢!

1 个答案:

答案 0 :(得分:3)

您可以将3个视图约束为具有相等的宽度,然后添加一些约束为恒定宽度的分隔视图。

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

let view1 = UIView()
view1.backgroundColor = .red
stackView.addArrangedSubview(view1)

let separator1 = UIView()
separator1.backgroundColor = .black
stackView.addArrangedSubview(separator1)
separator1.widthAnchor.constraint(equalToConstant: 1).isActive = true

let view2 = UIView()
view2.backgroundColor = .green
stackView.addArrangedSubview(view2)
view2.widthAnchor.constraint(equalTo: view1.widthAnchor, multiplier: 1).isActive = true

let separator2 = UIView()
separator2.backgroundColor = .black
stackView.addArrangedSubview(separator2)
separator2.widthAnchor.constraint(equalToConstant: 1).isActive = true

let view3 = UIView()
view3.backgroundColor = .blue
stackView.addArrangedSubview(view3)
view3.widthAnchor.constraint(equalTo: view1.widthAnchor, multiplier: 1).isActive = true