isBaselineRelativeArrangement 属性 在 UIStackView 对齐过程中实际做了什么?
What does actually doing isBaselineRelativeArrangement property in UIStackView alignment process?
Apple 文档说:
If YES, the vertical space between views are measured from the last
baseline of a text-based view, to the first baseline of the view below
it. Top and bottom views are also positioned so that their closest
baseline is the specified distance away from the stack view’s edge.
This property is only used by vertical stack views. Use the alignment
property to baseline align views in a horizontal stack view.
我试图在代码中看到这一点。我创建了由 3 个 UIView 组成的简单 stackView。在每个 UIView 中都有一个带有虚拟文本的 UITextView。
override func viewDidLoad() {
super.viewDidLoad()
let saveButton = UIView()
saveButton.backgroundColor = UIColor.green
saveButton.translatesAutoresizingMaskIntoConstraints = false
let saveButtonTextView = UITextView(frame: CGRect(0, 0, 100, 50))
saveButtonTextView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
saveButtonTextView.backgroundColor = UIColor.clear
saveButton.addSubview(saveButtonTextView)
let revertButton = UIView()
revertButton.backgroundColor = UIColor.yellow
revertButton.translatesAutoresizingMaskIntoConstraints = false
let revertButtonTextView = UITextView(frame: CGRect(0, 0, 100, 50))
revertButtonTextView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
revertButtonTextView.backgroundColor = UIColor.clear
revertButton.addSubview(revertButtonTextView)
let cancelButton = UIView()
cancelButton.backgroundColor = UIColor.red
cancelButton.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView = UITextView(frame: CGRect(0, 0, 100, 50))
cancelButtonRevertView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
cancelButtonRevertView.backgroundColor = UIColor.clear
cancelButton.addSubview(cancelButtonRevertView)
let views = [saveButton,revertButton,cancelButton]
saveButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
revertButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
cancelButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
// give the stack view arranged subviews
let sv = UIStackView(arrangedSubviews: views)
// configure the stack view
sv.axis = .vertical
sv.alignment = .fill
sv.distribution = .equalSpacing
// constrain the stack view
sv.translatesAutoresizingMaskIntoConstraints = false
sv.isBaselineRelativeArrangement = true
self.view.addSubview(sv)
let marg = self.view.layoutMarginsGuide
let safe = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo:safe.topAnchor, constant: 200),
sv.leadingAnchor.constraint(equalTo:marg.leadingAnchor,constant: 100), sv.trailingAnchor.constraint(equalTo:marg.trailingAnchor,constant: -100), //sv.bottomAnchor.constraint(equalTo:safe.bottomAnchor, constant: -200),
])
}
我试图将此 isBaselineRelativeArrangement 更改为 false 和 true,但似乎没有任何改变。也许我误解了什么?
更新:@matt 在下面评论道:“您的堆栈视图不够高,无法区分;标签间距为零。”
我尝试添加视图并将 UITextView 替换为 UILabel 以使行距大于 0。
这是代码:
override func viewDidLoad() {
super.viewDidLoad()
let saveButton = UIView()
saveButton.backgroundColor = UIColor.green
saveButton.translatesAutoresizingMaskIntoConstraints = false
let saveButtonTextView = UITextView(frame: CGRect(0, 0, 100, 130))
saveButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
saveButtonTextView.backgroundColor = UIColor.clear
saveButton.addSubview(saveButtonTextView)
let revertButton = UIView()
revertButton.backgroundColor = UIColor.yellow
revertButton.translatesAutoresizingMaskIntoConstraints = false
let revertButtonTextView = UITextView(frame: CGRect(0, 0, 100, 130))
revertButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
revertButtonTextView.backgroundColor = UIColor.clear
revertButton.addSubview(revertButtonTextView)
let cancelButton = UIView()
cancelButton.backgroundColor = UIColor.red
cancelButton.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView = UITextView(frame: CGRect(0, 0, 100, 130))
cancelButtonRevertView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
cancelButtonRevertView.backgroundColor = UIColor.clear
cancelButton.addSubview(cancelButtonRevertView)
let cancelButton1 = UIView()
cancelButton1.backgroundColor = UIColor.orange
cancelButton1.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView1 = UILabel(frame: CGRect(0, 0, 100, 130))
let textForLabe1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle1 = NSMutableParagraphStyle()
//line height size
paragraphStyle1.lineSpacing = 7
let attrString1 = NSMutableAttributedString(string: textForLabe1)
attrString1.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle1, range:NSMakeRange(0, attrString1.length))
attrString1.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString1.length))
cancelButtonRevertView1.numberOfLines = 0
cancelButtonRevertView1.attributedText = attrString1
cancelButtonRevertView1.backgroundColor = UIColor.clear
cancelButton1.addSubview(cancelButtonRevertView1)
let cancelButton2 = UIView()
cancelButton2.backgroundColor = UIColor.cyan
cancelButton2.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView2 = UILabel(frame: CGRect(0, 0, 100, 130))
let textForLabel2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle2 = NSMutableParagraphStyle()
//line height size
paragraphStyle2.lineSpacing = 7
let attrString2 = NSMutableAttributedString(string: textForLabel2)
attrString2.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle2, range:NSMakeRange(0, attrString2.length))
attrString2.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString2.length))
cancelButtonRevertView2.numberOfLines = 0
cancelButtonRevertView2.attributedText = attrString2
cancelButtonRevertView2.backgroundColor = UIColor.clear
cancelButton2.addSubview(cancelButtonRevertView2)
let cancelButton3 = UIView()
cancelButton3.backgroundColor = UIColor.cyan
cancelButton3.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView3 = UILabel(frame: CGRect(30, 0, 100, 130))
let textForLabel = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle = NSMutableParagraphStyle()
//line height size
paragraphStyle.lineSpacing = 7
let attrString = NSMutableAttributedString(string: textForLabel)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
attrString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString.length))
cancelButtonRevertView3.numberOfLines = 0
cancelButtonRevertView3.attributedText = attrString
cancelButtonRevertView3.backgroundColor = UIColor.clear
cancelButton3.addSubview(cancelButtonRevertView3)
let views = [saveButton,revertButton,cancelButton3,cancelButton,cancelButton1,cancelButton2]
saveButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
revertButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton1.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton2.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton3.heightAnchor.constraint(equalToConstant: 120).isActive = true
// give the stack view arranged subviews
let sv = UIStackView(arrangedSubviews: views)
// configure the stack view
sv.axis = .vertical
//sv.alignment = .fill
sv.distribution = .equalSpacing
// constrain the stack view
sv.translatesAutoresizingMaskIntoConstraints = false
sv.isBaselineRelativeArrangement = true
self.view.addSubview(sv)
let marg = self.view.layoutMarginsGuide
let safe = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo:safe.topAnchor, constant: 0),
sv.leadingAnchor.constraint(equalTo:marg.leadingAnchor,constant: 0), sv.trailingAnchor.constraint(equalTo:marg.trailingAnchor,constant: 0)
//, //sv.bottomAnchor.constraint(equalTo:safe.bottomAnchor, constant: -200),
])
}
我再次切换 属性,但似乎没有任何变化 :(
更新:@matt 在下面评论道:您仍然没有做任何实际间距,因此没有“视图之间的垂直 space”可谈。看到了吗?
嗯,我调整了我的代码,所以 stackView 的子视图之间会有垂直 space。
override func viewDidLoad() {
[![enter image description here][3]][3] super.viewDidLoad()
let saveButton = UIView()
saveButton.backgroundColor = UIColor.green
saveButton.translatesAutoresizingMaskIntoConstraints = false
let saveButtonTextView = UITextView(frame: CGRect(0, 0, 100, 130))
saveButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
saveButtonTextView.backgroundColor = UIColor.clear
saveButton.addSubview(saveButtonTextView)
let revertButton = UIView()
revertButton.backgroundColor = UIColor.yellow
revertButton.translatesAutoresizingMaskIntoConstraints = false
let revertButtonTextView = UITextView(frame: CGRect(0, 0, 100, 130))
revertButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
revertButtonTextView.backgroundColor = UIColor.clear
revertButton.addSubview(revertButtonTextView)
let cancelButton = UIView()
cancelButton.backgroundColor = UIColor.red
cancelButton.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView = UITextView(frame: CGRect(0, 0, 100, 130))
cancelButtonRevertView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
cancelButtonRevertView.backgroundColor = UIColor.clear
cancelButton.addSubview(cancelButtonRevertView)
let cancelButton1 = UIView()
cancelButton1.backgroundColor = UIColor.orange
cancelButton1.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView1 = UILabel(frame: CGRect(0, 0, 100, 130))
let textForLabe1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle1 = NSMutableParagraphStyle()
//line height size
paragraphStyle1.lineSpacing = 7
let attrString1 = NSMutableAttributedString(string: textForLabe1)
attrString1.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle1, range:NSMakeRange(0, attrString1.length))
attrString1.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString1.length))
cancelButtonRevertView1.numberOfLines = 0
cancelButtonRevertView1.attributedText = attrString1
cancelButtonRevertView1.backgroundColor = UIColor.clear
cancelButton1.addSubview(cancelButtonRevertView1)
let cancelButton2 = UIView()
cancelButton2.backgroundColor = UIColor.cyan
cancelButton2.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView2 = UILabel(frame: CGRect(0, 0, 100, 130))
let textForLabel2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle2 = NSMutableParagraphStyle()
//line height size
paragraphStyle2.lineSpacing = 7
let attrString2 = NSMutableAttributedString(string: textForLabel2)
attrString2.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle2, range:NSMakeRange(0, attrString2.length))
attrString2.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString2.length))
cancelButtonRevertView2.numberOfLines = 0
cancelButtonRevertView2.attributedText = attrString2
cancelButtonRevertView2.backgroundColor = UIColor.clear
cancelButton2.addSubview(cancelButtonRevertView2)
let cancelButton3 = UIView()
cancelButton3.backgroundColor = UIColor.cyan
cancelButton3.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView3 = UILabel(frame: CGRect(30, 0, 100, 130))
let textForLabel = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle = NSMutableParagraphStyle()
//line height size
paragraphStyle.lineSpacing = 7
let attrString = NSMutableAttributedString(string: textForLabel)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
attrString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString.length))
cancelButtonRevertView3.numberOfLines = 0
cancelButtonRevertView3.attributedText = attrString
cancelButtonRevertView3.backgroundColor = UIColor.clear
cancelButton3.addSubview(cancelButtonRevertView3)
let views = [ saveButton, cancelButton3,cancelButton,cancelButton1,cancelButton2]
saveButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton1.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton2.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton3.heightAnchor.constraint(equalToConstant: 120).isActive = true
// give the stack view arranged subviews
let sv = UIStackView(arrangedSubviews: views)
// configure the stack view
sv.axis = .vertical
//sv.alignment = .fill
sv.distribution = .equalSpacing
// constrain the stack view
sv.translatesAutoresizingMaskIntoConstraints = false
//sv.isBaselineRelativeArrangement = true
self.view.addSubview(sv)
let marg = self.view.layoutMarginsGuide
let safe = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo:safe.topAnchor, constant: 0),
sv.leadingAnchor.constraint(equalTo:marg.leadingAnchor,constant: 0), sv.trailingAnchor.constraint(equalTo:marg.trailingAnchor,constant: 0),
sv.bottomAnchor.constraint(equalTo:safe.bottomAnchor, constant: 0)
])
但仍然...当我将此 属性 从 false 切换为 true 时没有任何变化。我真的认为我不明白一些关键的想法,但我不知道这个想法是什么。 ((
好吧,在对这个问题进行了一番“敲打”之后,我想我发现了在 UIStackView 对齐过程中显示此 isBaselineRelativeArrangement 属性 的众多更改之一。
现在,我尝试不设置对齐 属性,而是直接设置间距 属性。
let saveButton = UIView()
saveButton.backgroundColor = UIColor.green
saveButton.translatesAutoresizingMaskIntoConstraints = false
let saveButtonTextView = UITextView(frame: CGRect(0, 10, 100, 130))
saveButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
saveButtonTextView.backgroundColor = UIColor.clear
saveButton.addSubview(saveButtonTextView)
let revertButton = UIView()
revertButton.backgroundColor = UIColor.yellow
revertButton.translatesAutoresizingMaskIntoConstraints = false
let revertButtonTextView = UITextView(frame: CGRect(15, 10, 100, 130))
revertButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
revertButtonTextView.backgroundColor = UIColor.clear
revertButton.addSubview(revertButtonTextView)
let cancelButton = UIView()
cancelButton.backgroundColor = UIColor.red
cancelButton.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView = UITextView(frame: CGRect(0, 20, 100, 130))
cancelButtonRevertView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
cancelButtonRevertView.backgroundColor = UIColor.clear
cancelButton.addSubview(cancelButtonRevertView)
let cancelButton1 = UIView()
cancelButton1.backgroundColor = UIColor.orange
cancelButton1.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView1 = UILabel(frame: CGRect(20, 20, 100, 130))
let textForLabe1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle1 = NSMutableParagraphStyle()
//line height size
paragraphStyle1.lineSpacing = 7
let attrString1 = NSMutableAttributedString(string: textForLabe1)
attrString1.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle1, range:NSMakeRange(0, attrString1.length))
attrString1.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString1.length))
cancelButtonRevertView1.numberOfLines = 0
cancelButtonRevertView1.attributedText = attrString1
cancelButtonRevertView1.backgroundColor = UIColor.red
cancelButton1.addSubview(cancelButtonRevertView1)
let cancelButton2 = UIView()
cancelButton2.backgroundColor = UIColor.cyan
cancelButton2.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView2 = UILabel(frame: CGRect(60, 20, 100, 130))
let textForLabel2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle2 = NSMutableParagraphStyle()
//line height size
paragraphStyle2.lineSpacing = 7
let attrString2 = NSMutableAttributedString(string: textForLabel2)
attrString2.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle2, range:NSMakeRange(0, attrString2.length))
attrString2.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString2.length))
cancelButtonRevertView2.numberOfLines = 0
cancelButtonRevertView2.attributedText = attrString2
cancelButtonRevertView2.backgroundColor = UIColor.green
cancelButton2.addSubview(cancelButtonRevertView2)
let cancelButton3 = UIView()
cancelButton3.backgroundColor = UIColor.cyan
cancelButton3.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView3 = UILabel(frame: CGRect(600, 20, 200, 130))
let textForLabel = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle = NSMutableParagraphStyle()
//line height size
paragraphStyle.lineSpacing = 7
let attrString = NSMutableAttributedString(string: textForLabel)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
attrString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString.length))
cancelButtonRevertView3.numberOfLines = 0
cancelButtonRevertView3.attributedText = attrString
cancelButtonRevertView3.backgroundColor = UIColor.blue
cancelButton3.addSubview(cancelButtonRevertView3)
let views = [saveButton,cancelButtonRevertView1, cancelButtonRevertView2, cancelButtonRevertView3]
let sv = UIStackView(arrangedSubviews: views)
sv.axis = .vertical
sv.spacing = 58.0
sv.translatesAutoresizingMaskIntoConstraints = false
sv.isBaselineRelativeArrangement = false
self.view.addSubview(sv)
let marg = self.view.layoutMarginsGuide
let safe = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo:safe.topAnchor, constant: 0),
sv.leadingAnchor.constraint(equalTo:marg.leadingAnchor,constant: 0), sv.trailingAnchor.constraint(equalTo:marg.trailingAnchor,constant: 0),
sv.bottomAnchor.constraint(equalTo:safe.bottomAnchor, constant: 0),
])
那么! A 将 sv.isBaselineRelativeArrangement 更改为等于 true。而这个 属性 真正开始体现 stackView 布局的变化!
太棒了。但我可能是错的。
这是一个显示差异的示例:
override func viewDidLoad() {
super.viewDidLoad()
let stack = UIStackView()
stack.axis = .vertical
stack.alignment = .fill
stack.distribution = .fill
stack.isBaselineRelativeArrangement = false
self.view.addSubview(stack)
stack.translatesAutoresizingMaskIntoConstraints = false
stack.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
stack.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
stack.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
stack.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
let s = "thank for looking at the question I changed the code again to meet this requirement and updated the question and posted screenshot of the results, please look at the question thank for looking at the question I changed the code again to meet this requirement and updated the question and posted screenshot of the results, please look at the question"
let lab1 = UILabel()
lab1.numberOfLines = 0
lab1.text = s
stack.addArrangedSubview(lab1)
lab1.setContentHuggingPriority(UILayoutPriority(100), for: .vertical)
let lab2 = UILabel()
lab2.numberOfLines = 0
lab2.text = "testing\ntesting\ntesting\ntesting\ntesting"
stack.addArrangedSubview(lab2)
let lab3 = UILabel()
lab3.numberOfLines = 0
lab3.text = s
stack.addArrangedSubview(lab3)
// comment this out for an even more dramatic effect
stack.setCustomSpacing(30, after: lab2)
}
尝试使用 stack.isBaselineRelativeArrangement
的不同值。您会看到第二个和第三个标签之间的 space 的测量方式不同。
Apple 文档说:
If YES, the vertical space between views are measured from the last baseline of a text-based view, to the first baseline of the view below it. Top and bottom views are also positioned so that their closest baseline is the specified distance away from the stack view’s edge. This property is only used by vertical stack views. Use the alignment property to baseline align views in a horizontal stack view.
我试图在代码中看到这一点。我创建了由 3 个 UIView 组成的简单 stackView。在每个 UIView 中都有一个带有虚拟文本的 UITextView。
override func viewDidLoad() {
super.viewDidLoad()
let saveButton = UIView()
saveButton.backgroundColor = UIColor.green
saveButton.translatesAutoresizingMaskIntoConstraints = false
let saveButtonTextView = UITextView(frame: CGRect(0, 0, 100, 50))
saveButtonTextView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
saveButtonTextView.backgroundColor = UIColor.clear
saveButton.addSubview(saveButtonTextView)
let revertButton = UIView()
revertButton.backgroundColor = UIColor.yellow
revertButton.translatesAutoresizingMaskIntoConstraints = false
let revertButtonTextView = UITextView(frame: CGRect(0, 0, 100, 50))
revertButtonTextView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
revertButtonTextView.backgroundColor = UIColor.clear
revertButton.addSubview(revertButtonTextView)
let cancelButton = UIView()
cancelButton.backgroundColor = UIColor.red
cancelButton.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView = UITextView(frame: CGRect(0, 0, 100, 50))
cancelButtonRevertView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
cancelButtonRevertView.backgroundColor = UIColor.clear
cancelButton.addSubview(cancelButtonRevertView)
let views = [saveButton,revertButton,cancelButton]
saveButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
revertButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
cancelButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
// give the stack view arranged subviews
let sv = UIStackView(arrangedSubviews: views)
// configure the stack view
sv.axis = .vertical
sv.alignment = .fill
sv.distribution = .equalSpacing
// constrain the stack view
sv.translatesAutoresizingMaskIntoConstraints = false
sv.isBaselineRelativeArrangement = true
self.view.addSubview(sv)
let marg = self.view.layoutMarginsGuide
let safe = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo:safe.topAnchor, constant: 200),
sv.leadingAnchor.constraint(equalTo:marg.leadingAnchor,constant: 100), sv.trailingAnchor.constraint(equalTo:marg.trailingAnchor,constant: -100), //sv.bottomAnchor.constraint(equalTo:safe.bottomAnchor, constant: -200),
])
}
我试图将此 isBaselineRelativeArrangement 更改为 false 和 true,但似乎没有任何改变。也许我误解了什么?
更新:@matt 在下面评论道:“您的堆栈视图不够高,无法区分;标签间距为零。” 我尝试添加视图并将 UITextView 替换为 UILabel 以使行距大于 0。 这是代码:
override func viewDidLoad() {
super.viewDidLoad()
let saveButton = UIView()
saveButton.backgroundColor = UIColor.green
saveButton.translatesAutoresizingMaskIntoConstraints = false
let saveButtonTextView = UITextView(frame: CGRect(0, 0, 100, 130))
saveButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
saveButtonTextView.backgroundColor = UIColor.clear
saveButton.addSubview(saveButtonTextView)
let revertButton = UIView()
revertButton.backgroundColor = UIColor.yellow
revertButton.translatesAutoresizingMaskIntoConstraints = false
let revertButtonTextView = UITextView(frame: CGRect(0, 0, 100, 130))
revertButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
revertButtonTextView.backgroundColor = UIColor.clear
revertButton.addSubview(revertButtonTextView)
let cancelButton = UIView()
cancelButton.backgroundColor = UIColor.red
cancelButton.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView = UITextView(frame: CGRect(0, 0, 100, 130))
cancelButtonRevertView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
cancelButtonRevertView.backgroundColor = UIColor.clear
cancelButton.addSubview(cancelButtonRevertView)
let cancelButton1 = UIView()
cancelButton1.backgroundColor = UIColor.orange
cancelButton1.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView1 = UILabel(frame: CGRect(0, 0, 100, 130))
let textForLabe1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle1 = NSMutableParagraphStyle()
//line height size
paragraphStyle1.lineSpacing = 7
let attrString1 = NSMutableAttributedString(string: textForLabe1)
attrString1.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle1, range:NSMakeRange(0, attrString1.length))
attrString1.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString1.length))
cancelButtonRevertView1.numberOfLines = 0
cancelButtonRevertView1.attributedText = attrString1
cancelButtonRevertView1.backgroundColor = UIColor.clear
cancelButton1.addSubview(cancelButtonRevertView1)
let cancelButton2 = UIView()
cancelButton2.backgroundColor = UIColor.cyan
cancelButton2.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView2 = UILabel(frame: CGRect(0, 0, 100, 130))
let textForLabel2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle2 = NSMutableParagraphStyle()
//line height size
paragraphStyle2.lineSpacing = 7
let attrString2 = NSMutableAttributedString(string: textForLabel2)
attrString2.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle2, range:NSMakeRange(0, attrString2.length))
attrString2.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString2.length))
cancelButtonRevertView2.numberOfLines = 0
cancelButtonRevertView2.attributedText = attrString2
cancelButtonRevertView2.backgroundColor = UIColor.clear
cancelButton2.addSubview(cancelButtonRevertView2)
let cancelButton3 = UIView()
cancelButton3.backgroundColor = UIColor.cyan
cancelButton3.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView3 = UILabel(frame: CGRect(30, 0, 100, 130))
let textForLabel = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle = NSMutableParagraphStyle()
//line height size
paragraphStyle.lineSpacing = 7
let attrString = NSMutableAttributedString(string: textForLabel)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
attrString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString.length))
cancelButtonRevertView3.numberOfLines = 0
cancelButtonRevertView3.attributedText = attrString
cancelButtonRevertView3.backgroundColor = UIColor.clear
cancelButton3.addSubview(cancelButtonRevertView3)
let views = [saveButton,revertButton,cancelButton3,cancelButton,cancelButton1,cancelButton2]
saveButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
revertButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton1.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton2.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton3.heightAnchor.constraint(equalToConstant: 120).isActive = true
// give the stack view arranged subviews
let sv = UIStackView(arrangedSubviews: views)
// configure the stack view
sv.axis = .vertical
//sv.alignment = .fill
sv.distribution = .equalSpacing
// constrain the stack view
sv.translatesAutoresizingMaskIntoConstraints = false
sv.isBaselineRelativeArrangement = true
self.view.addSubview(sv)
let marg = self.view.layoutMarginsGuide
let safe = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo:safe.topAnchor, constant: 0),
sv.leadingAnchor.constraint(equalTo:marg.leadingAnchor,constant: 0), sv.trailingAnchor.constraint(equalTo:marg.trailingAnchor,constant: 0)
//, //sv.bottomAnchor.constraint(equalTo:safe.bottomAnchor, constant: -200),
])
}
我再次切换 属性,但似乎没有任何变化 :(
更新:@matt 在下面评论道:您仍然没有做任何实际间距,因此没有“视图之间的垂直 space”可谈。看到了吗?
嗯,我调整了我的代码,所以 stackView 的子视图之间会有垂直 space。
override func viewDidLoad() {
[![enter image description here][3]][3] super.viewDidLoad()
let saveButton = UIView()
saveButton.backgroundColor = UIColor.green
saveButton.translatesAutoresizingMaskIntoConstraints = false
let saveButtonTextView = UITextView(frame: CGRect(0, 0, 100, 130))
saveButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
saveButtonTextView.backgroundColor = UIColor.clear
saveButton.addSubview(saveButtonTextView)
let revertButton = UIView()
revertButton.backgroundColor = UIColor.yellow
revertButton.translatesAutoresizingMaskIntoConstraints = false
let revertButtonTextView = UITextView(frame: CGRect(0, 0, 100, 130))
revertButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
revertButtonTextView.backgroundColor = UIColor.clear
revertButton.addSubview(revertButtonTextView)
let cancelButton = UIView()
cancelButton.backgroundColor = UIColor.red
cancelButton.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView = UITextView(frame: CGRect(0, 0, 100, 130))
cancelButtonRevertView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
cancelButtonRevertView.backgroundColor = UIColor.clear
cancelButton.addSubview(cancelButtonRevertView)
let cancelButton1 = UIView()
cancelButton1.backgroundColor = UIColor.orange
cancelButton1.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView1 = UILabel(frame: CGRect(0, 0, 100, 130))
let textForLabe1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle1 = NSMutableParagraphStyle()
//line height size
paragraphStyle1.lineSpacing = 7
let attrString1 = NSMutableAttributedString(string: textForLabe1)
attrString1.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle1, range:NSMakeRange(0, attrString1.length))
attrString1.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString1.length))
cancelButtonRevertView1.numberOfLines = 0
cancelButtonRevertView1.attributedText = attrString1
cancelButtonRevertView1.backgroundColor = UIColor.clear
cancelButton1.addSubview(cancelButtonRevertView1)
let cancelButton2 = UIView()
cancelButton2.backgroundColor = UIColor.cyan
cancelButton2.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView2 = UILabel(frame: CGRect(0, 0, 100, 130))
let textForLabel2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle2 = NSMutableParagraphStyle()
//line height size
paragraphStyle2.lineSpacing = 7
let attrString2 = NSMutableAttributedString(string: textForLabel2)
attrString2.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle2, range:NSMakeRange(0, attrString2.length))
attrString2.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString2.length))
cancelButtonRevertView2.numberOfLines = 0
cancelButtonRevertView2.attributedText = attrString2
cancelButtonRevertView2.backgroundColor = UIColor.clear
cancelButton2.addSubview(cancelButtonRevertView2)
let cancelButton3 = UIView()
cancelButton3.backgroundColor = UIColor.cyan
cancelButton3.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView3 = UILabel(frame: CGRect(30, 0, 100, 130))
let textForLabel = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle = NSMutableParagraphStyle()
//line height size
paragraphStyle.lineSpacing = 7
let attrString = NSMutableAttributedString(string: textForLabel)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
attrString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString.length))
cancelButtonRevertView3.numberOfLines = 0
cancelButtonRevertView3.attributedText = attrString
cancelButtonRevertView3.backgroundColor = UIColor.clear
cancelButton3.addSubview(cancelButtonRevertView3)
let views = [ saveButton, cancelButton3,cancelButton,cancelButton1,cancelButton2]
saveButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton1.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton2.heightAnchor.constraint(equalToConstant: 120).isActive = true
cancelButton3.heightAnchor.constraint(equalToConstant: 120).isActive = true
// give the stack view arranged subviews
let sv = UIStackView(arrangedSubviews: views)
// configure the stack view
sv.axis = .vertical
//sv.alignment = .fill
sv.distribution = .equalSpacing
// constrain the stack view
sv.translatesAutoresizingMaskIntoConstraints = false
//sv.isBaselineRelativeArrangement = true
self.view.addSubview(sv)
let marg = self.view.layoutMarginsGuide
let safe = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo:safe.topAnchor, constant: 0),
sv.leadingAnchor.constraint(equalTo:marg.leadingAnchor,constant: 0), sv.trailingAnchor.constraint(equalTo:marg.trailingAnchor,constant: 0),
sv.bottomAnchor.constraint(equalTo:safe.bottomAnchor, constant: 0)
])
但仍然...当我将此 属性 从 false 切换为 true 时没有任何变化。我真的认为我不明白一些关键的想法,但我不知道这个想法是什么。 ((
好吧,在对这个问题进行了一番“敲打”之后,我想我发现了在 UIStackView 对齐过程中显示此 isBaselineRelativeArrangement 属性 的众多更改之一。
现在,我尝试不设置对齐 属性,而是直接设置间距 属性。
let saveButton = UIView()
saveButton.backgroundColor = UIColor.green
saveButton.translatesAutoresizingMaskIntoConstraints = false
let saveButtonTextView = UITextView(frame: CGRect(0, 10, 100, 130))
saveButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
saveButtonTextView.backgroundColor = UIColor.clear
saveButton.addSubview(saveButtonTextView)
let revertButton = UIView()
revertButton.backgroundColor = UIColor.yellow
revertButton.translatesAutoresizingMaskIntoConstraints = false
let revertButtonTextView = UITextView(frame: CGRect(15, 10, 100, 130))
revertButtonTextView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. "
revertButtonTextView.backgroundColor = UIColor.clear
revertButton.addSubview(revertButtonTextView)
let cancelButton = UIView()
cancelButton.backgroundColor = UIColor.red
cancelButton.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView = UITextView(frame: CGRect(0, 20, 100, 130))
cancelButtonRevertView.text = "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
cancelButtonRevertView.backgroundColor = UIColor.clear
cancelButton.addSubview(cancelButtonRevertView)
let cancelButton1 = UIView()
cancelButton1.backgroundColor = UIColor.orange
cancelButton1.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView1 = UILabel(frame: CGRect(20, 20, 100, 130))
let textForLabe1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle1 = NSMutableParagraphStyle()
//line height size
paragraphStyle1.lineSpacing = 7
let attrString1 = NSMutableAttributedString(string: textForLabe1)
attrString1.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle1, range:NSMakeRange(0, attrString1.length))
attrString1.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString1.length))
cancelButtonRevertView1.numberOfLines = 0
cancelButtonRevertView1.attributedText = attrString1
cancelButtonRevertView1.backgroundColor = UIColor.red
cancelButton1.addSubview(cancelButtonRevertView1)
let cancelButton2 = UIView()
cancelButton2.backgroundColor = UIColor.cyan
cancelButton2.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView2 = UILabel(frame: CGRect(60, 20, 100, 130))
let textForLabel2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle2 = NSMutableParagraphStyle()
//line height size
paragraphStyle2.lineSpacing = 7
let attrString2 = NSMutableAttributedString(string: textForLabel2)
attrString2.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle2, range:NSMakeRange(0, attrString2.length))
attrString2.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString2.length))
cancelButtonRevertView2.numberOfLines = 0
cancelButtonRevertView2.attributedText = attrString2
cancelButtonRevertView2.backgroundColor = UIColor.green
cancelButton2.addSubview(cancelButtonRevertView2)
let cancelButton3 = UIView()
cancelButton3.backgroundColor = UIColor.cyan
cancelButton3.translatesAutoresizingMaskIntoConstraints = false
let cancelButtonRevertView3 = UILabel(frame: CGRect(600, 20, 200, 130))
let textForLabel = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic types"
let paragraphStyle = NSMutableParagraphStyle()
//line height size
paragraphStyle.lineSpacing = 7
let attrString = NSMutableAttributedString(string: textForLabel)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
attrString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attrString.length))
cancelButtonRevertView3.numberOfLines = 0
cancelButtonRevertView3.attributedText = attrString
cancelButtonRevertView3.backgroundColor = UIColor.blue
cancelButton3.addSubview(cancelButtonRevertView3)
let views = [saveButton,cancelButtonRevertView1, cancelButtonRevertView2, cancelButtonRevertView3]
let sv = UIStackView(arrangedSubviews: views)
sv.axis = .vertical
sv.spacing = 58.0
sv.translatesAutoresizingMaskIntoConstraints = false
sv.isBaselineRelativeArrangement = false
self.view.addSubview(sv)
let marg = self.view.layoutMarginsGuide
let safe = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo:safe.topAnchor, constant: 0),
sv.leadingAnchor.constraint(equalTo:marg.leadingAnchor,constant: 0), sv.trailingAnchor.constraint(equalTo:marg.trailingAnchor,constant: 0),
sv.bottomAnchor.constraint(equalTo:safe.bottomAnchor, constant: 0),
])
那么! A 将 sv.isBaselineRelativeArrangement 更改为等于 true。而这个 属性 真正开始体现 stackView 布局的变化!
太棒了。但我可能是错的。
这是一个显示差异的示例:
override func viewDidLoad() {
super.viewDidLoad()
let stack = UIStackView()
stack.axis = .vertical
stack.alignment = .fill
stack.distribution = .fill
stack.isBaselineRelativeArrangement = false
self.view.addSubview(stack)
stack.translatesAutoresizingMaskIntoConstraints = false
stack.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
stack.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
stack.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
stack.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
let s = "thank for looking at the question I changed the code again to meet this requirement and updated the question and posted screenshot of the results, please look at the question thank for looking at the question I changed the code again to meet this requirement and updated the question and posted screenshot of the results, please look at the question"
let lab1 = UILabel()
lab1.numberOfLines = 0
lab1.text = s
stack.addArrangedSubview(lab1)
lab1.setContentHuggingPriority(UILayoutPriority(100), for: .vertical)
let lab2 = UILabel()
lab2.numberOfLines = 0
lab2.text = "testing\ntesting\ntesting\ntesting\ntesting"
stack.addArrangedSubview(lab2)
let lab3 = UILabel()
lab3.numberOfLines = 0
lab3.text = s
stack.addArrangedSubview(lab3)
// comment this out for an even more dramatic effect
stack.setCustomSpacing(30, after: lab2)
}
尝试使用 stack.isBaselineRelativeArrangement
的不同值。您会看到第二个和第三个标签之间的 space 的测量方式不同。