我一直在使用Swift教程跟踪Paul的黑客行为,我准备使用项目6,他以编程方式使用布局约束。我一直只使用Interface来完成这类任务,但我很想了解如何以编程的方式完成它。
从他的教程中,我们有下面的代码,将5 UILabels添加到主控制器的视图中。
let label1 = UILabel()
label1.translatesAutoresizingMaskIntoConstraints = false
label1.text = "THESE"
label1.backgroundColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
label1.sizeToFit()
// do the same with label2, label3, label4, label5
view.addSubview(label1)
view.addSubview(label2)
view.addSubview(label3)
view.addSubview(label4)
view.addSubview(label5)然后我可以手动添加约束:
let dictionary = [
"label1": label1,
"label2": label2,
"label3": label3,
"label4": label4,
"label5": label5
]
let metrics=[ "labelHeight":80]
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[label1(labelHeight@999)]-[label2(label1)]-[label3(label1)]-[label4(label1)]-[label5(label1)]-(>=10)-|", options: [], metrics: metrics, views: dictionary))
for label in dictionary.keys {
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[\(label)]|", options: [], metrics:nil, views: dictionary))
}正如你所看到的,我把第一个标签的高度设置为80。然后,将label1设置为具有999的优先级,并使其余的标签遵循labels 1的高度约束。
这是很好的工作,在肖像和景观模式。


现在,我将代码转换为使用锚点。
let heightConstraint = label1.heightAnchor.constraint(equalToConstant: 88)
heightConstraint.priority = UILayoutPriority(rawValue: 999)
heightConstraint.isActive = true
for label in [label2, label3, label4, label5] {
label.heightAnchor.constraint(equalTo: label1.heightAnchor, multiplier: 1).isActive = true
}
var previousLabel : UILabel?
for label in [label1, label2, label3, label4, label5] {
label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
label.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
if let previousLabel = previousLabel {
label.topAnchor.constraint(equalTo: previousLabel.bottomAnchor, constant: 10).isActive = true
} else {
label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
}
previousLabel = label
}
label5.bottomAnchor.constraint(greaterThanOrEqualTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 10.0).isActive = true我想我漏掉了什么,因为


我想我在用锚的时候遗漏了什么吗?我猜是这样的:
-(>=10)-但我不知道怎么用锚模式来做。如能提供任何帮助,将不胜感激!
发布于 2021-09-04 11:47:55
对于label5,应该将底部约束更改为:
label5.bottomAnchor.constraint(lessThanOrEqualTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10.0).isActive = true它应该是一个负数,因为你把它固定在锚上,它在下面(与之前的锚相反,你把它固定在当前锚之上的标签上)。对于负数,您需要使用lessThanOrEqualTo。

对于垂直布局,它也很好,因为约束是lessThanOrEqualTo。

发布于 2022-07-02 21:09:20
这个问题很老了,但是我也在关注Paul和Swift教程的黑客行为,我在Project 6中也遇到了同样的问题。但我想出来了,这对任何其他人都有帮助。
对于尾部和底部约束,值应该是负值view.safeAreaLayoutGuide.bottomAnchor和view.trailingAnchor.。据我所知,维度总是决定向右向下。
label5.bottomAnchor.constraint(greaterThanOrEqualTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 10.0).isActive = true使用这段代码,切分是非常正常的,因为通过输入一个正值(+10.0),您已经按照标签5的bottomAnchor将在safeAreaLayoutGuide bottomAnchor下的方式初始化该控件了10个单元。
那么,为什么label5看起来完全在视图中呢?因为label1是附加到safeAreaLayoutGuide上的,所以标签决定了彼此之间的垂直空间。可能在运行代码时,XCode会显示一些错误,而忽略一些约束。您可以尝试不确定特定的高度。您可以使用:
label1.heightAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true 对于safeAreaLayoutGuide:
label5.bottomAnchor.constraint(lessThanOrEqualTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10.0).isActive = truehttps://stackoverflow.com/questions/69054929
复制相似问题