[英]Adding Separator View for Horizontal StackView with Equal Width Labels
I am trying to get fixed width labels to be set in stackview and have a separator in between the label.我正在尝试在 stackview 中设置固定宽度的标签,并在标签之间设置一个分隔符。
However, I have tried many variations but can't get to have it exactly placed.但是,我尝试了许多变体,但无法将其准确放置。
Below is the code and screenshot I have used, but wanted to know if there is something much better?下面是我使用的代码和屏幕截图,但想知道是否有更好的东西? I tried using compression and resistance but it didn't worked the way I wanted, or maybe I am just not setting it right?
我尝试使用压缩和阻力,但它没有按我想要的方式工作,或者我只是没有正确设置?
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.alignment = .fill // .Leading .FirstBaseline .Center .Trailing .LastBaseline
stackView.distribution = .fill // .FillEqually .FillProportionally .EqualSpacing .EqualCentering
let label = UILabel(frame: .zero)
label.numberOfLines = 0
label.backgroundColor = .green
//label.setContentHuggingPriority(UILayoutPriority(999), for: .horizontal)
//label.widthAnchor.constraint(equalTo: stackView.widthAnchor, multiplier: 0.3).isActive = true
//label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 998), for: .horizontal)
label.text = "Label, Label, Label Label Label Label Label Label Label Label Label"
let label2 = UILabel(frame: .zero)
label2.numberOfLines = 0
label2.backgroundColor = .red
//label2.setContentHuggingPriority(UILayoutPriority(998), for: .horizontal)
//label2.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 750), for: .horizontal)
label2.text = "Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2"
let label3 = UILabel(frame: .zero)
label3.numberOfLines = 0
label3.backgroundColor = .blue
//label3.setContentHuggingPriority(UILayoutPriority(998), for: .horizontal)
//label3.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 750), for: .horizontal)
label3.text = "Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2"
let separator = UIView()
separator.widthAnchor.constraint(equalToConstant: 1).isActive = true
separator.backgroundColor = .black
//separator.setContentHuggingPriority(UILayoutPriority(249), for: .horizontal)
//separator.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 749), for: .horizontal)
//stackView.addArrangedSubview(separator)
//separator.heightAnchor.constraint(equalTo: stackView.heightAnchor, multiplier: 0.6).isActive = true
let separator2 = UIView()
separator2.widthAnchor.constraint(equalToConstant: 1).isActive = true
separator2.backgroundColor = .black
stackView.addArrangedSubview(label)
label.widthAnchor.constraint(equalTo: stackView.widthAnchor, multiplier: 0.333).isActive = true
stackView.addArrangedSubview(separator)
stackView.addArrangedSubview(label2)
label2.widthAnchor.constraint(equalTo: stackView.widthAnchor, multiplier: 0.333).isActive = true
stackView.addArrangedSubview(separator2)
stackView.addArrangedSubview(label3)
label3.widthAnchor.constraint(equalTo: stackView.widthAnchor, multiplier: 0.333).isActive = true
separator.heightAnchor.constraint(equalTo: stackView.heightAnchor).isActive = true
self.view.addSubview(stackView)
stackView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
stackView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 30).isActive = true
stackView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 30).isActive = true
Hey I'd propose to use UIKitPlus library嘿,我建议使用UIKitPlus 库
The trick is to use equal width for all three labels and for that you should link label#2 and label#3 widths to width of label#1.诀窍是对所有三个标签使用相等的宽度,为此您应该将标签#2 和标签#3 的宽度链接到标签#1 的宽度。 This is because you want a black separator view between them.
这是因为您想要它们之间的黑色分隔符视图。
import UIKitPlus
class ThreeLabelsViewController: ViewController {
var loremIpsumText: String { "Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups." }
lazy var text1 = Text(loremIpsumText)
override func buildUI() {
super.buildUI()
body {
HStack {
text1.background(.green).multiline()
HSpace(10).background(.black)
Text(loremIpsumText).background(.red).multiline().width(to: text1)
HSpace(10).background(.black)
Text(loremIpsumText).background(.blue).multiline().width(to: text1)
}
.edgesToSuperview()
}
}
}
Because without that separator view you can use StackView's spacing
and distribution
properties to do the same easily.因为没有该分隔符视图,您可以使用 StackView 的
spacing
和distribution
属性轻松完成相同的操作。
import UIKitPlus
class ThreeLabelsViewController: ViewController {
var loremIpsumText: String { "Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups." }
override func buildUI() {
super.buildUI()
view.backgroundColor = .black
body {
HStack {
Text(loremIpsumText).background(.green).multiline()
Text(loremIpsumText).background(.red).multiline()
Text(loremIpsumText).background(.blue).multiline()
}
.spacing(10)
.distribution(.fillEqually)
.edgesToSuperview()
}
}
}
And visible result is the same和可见的结果是一样的
This code is your solution?这段代码是你的解决方案吗?
StackView can auto resize width, you must be set separator width. StackView 可以自动调整宽度,您必须设置分隔符宽度。
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.alignment = .fill // .Leading .FirstBaseline .Center .Trailing .LastBaseline
stackView.distribution = .fill // .FillEqually .FillProportionally .EqualSpacing .EqualCentering
let label = UILabel(frame: .zero)
label.numberOfLines = 0
label.backgroundColor = .green
label.text = "Label, Label, Label Label Label Label Label Label Label Label Label"
let label2 = UILabel(frame: .zero)
label2.numberOfLines = 0
label2.backgroundColor = .red
label2.text = "Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2"
let label3 = UILabel(frame: .zero)
label3.numberOfLines = 0
label3.backgroundColor = .blue
label3.text = "Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2 Label2"
let separatorWidth: CGFloat = 10
let separator = UIView()
separator.widthAnchor.constraint(equalToConstant: separatorWidth).isActive = true
separator.backgroundColor = .black
let separator2 = UIView()
separator2.widthAnchor.constraint(equalToConstant: separatorWidth).isActive = true
separator2.backgroundColor = .black
stackView.addArrangedSubview(label)
stackView.addArrangedSubview(separator)
stackView.addArrangedSubview(label2)
stackView.addArrangedSubview(separator2)
stackView.addArrangedSubview(label3)
label2.widthAnchor.constraint(equalTo: label.widthAnchor).isActive = true
label3.widthAnchor.constraint(equalTo: label2.widthAnchor).isActive = true
self.view.addSubview(stackView)
stackView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
stackView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 30).isActive = true
stackView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 30).isActive = true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.