我希望以编程方式使用AutoLayout约束将彼此相邻的一组标签对齐。
注意:每个标签必须有不同的宽度。就像我需要分配数组[50,40,70,30,20,10]
我找到了一些相关的问题但在我的案例中没用;
for var i=0;i<7;i++{
let DataLabel = UILabel()
DataLabel.translatesAutoresizingMaskIntoConstraints = false
DataLabel.text = NamingLabel[i]
DataLabel.backgroundColor = UIColor.lightGrayColor()
Self.view.addSubview(DataLabel)
}
如果约束是在Visual Format中,语言意味着更有用。
先谢谢。
答案 0 :(得分:3)
您需要以下VFL字符串作为标签:
"V:|-40-[*labelName*]"
。将40
更改为最适合您的内容。 "H:|-[label0(50)][label1(40)][label2(70)][label3(30)][label4(20)][label5(10)][label6(30)]"
为了能够使用可视格式语言,您创建了一个包含所有标签的字典。该字典的值为UILabels
,并且您在VFL字符串中使用的键为Strings
,以定义应将约束添加到哪个标签。在这种情况下,我使用键&#34; label0&#34;,&#34; label1&#34;,&#34; label2&#34;等
以下是如何在循环中添加约束的示例:
let labelWidths = [50, 40, 70, 30, 20, 10, 30]
var labels = [String: UILabel]()
var allConstraints = [NSLayoutConstraint]()
var horizontalConstraintsString = "H:|-"
for i in 0..<7 {
let labelName = "label\(i)"
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
// set text, textColor etc.
view.addSubview(label)
labels[labelName] = label
// add vertical constraints to label
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[\(labelName)]", options: [], metrics: nil, views: labels)
allConstraints.appendContentsOf(verticalConstraints)
// add label to horizontal VFL string
horizontalConstraintsString += "[\(labelName)(\(labelWidths[i]))]"
}
// get horizontal contraints from horizontal VFL string
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(horizontalConstraintsString, options: [], metrics: nil, views: labels)
allConstraints.appendContentsOf(horizontalConstraints)
NSLayoutConstraint.activateConstraints(allConstraints)
<强>更新强>:
当你在评论中写道它仍然不适合你时,这是我的示例项目中的完整ViewController
类。所以只需在Xcode中创建一个新项目,然后将以下代码粘贴到ViewController
类中:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.lightGrayColor()
let labelWidths = [50, 40, 70, 30, 20, 10, 30]
var labels = [String: UILabel]()
var allConstraints = [NSLayoutConstraint]()
var horizontalConstraintsString = "H:|-"
for i in 0..<7 {
let labelName = "label\(i)"
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "\(i)"
label.layer.cornerRadius = 6
label.clipsToBounds = true
label.backgroundColor = UIColor.grayColor()
view.addSubview(label)
labels[labelName] = label
// add vertical constraints to label
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[\(labelName)]", options: [], metrics: nil, views: labels)
allConstraints.appendContentsOf(verticalConstraints)
// add label to horizontal VFL string
horizontalConstraintsString += "[\(labelName)(\(labelWidths[i]))]"
}
// get horizontal contraints from horizontal VFL string
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(horizontalConstraintsString, options: [], metrics: nil, views: labels)
allConstraints.appendContentsOf(horizontalConstraints)
NSLayoutConstraint.activateConstraints(allConstraints)
}
}
我在标签上添加了背景颜色和圆角半径,以便您可以看到一个标签的结束位置和下一个标签的开始位置。运行此项目时,您应该看到以下内容: