在 VFL 中创建垫片
Creating Spacers in VFL
AutoLayoutbyExample via Apple Developer Documentation
有人提供了如何创建间隔视图以在 VFL 中将标签之间的间距设置为相等的示例吗?
labelsDictionary = [ "label1": label1,
"label2": label2,
"label3": label3,
"label4": label4 ]
let metrics = ["edgeSpacing": 20, "spacingBetween": 5, "labelWidth": 50]
let constraints = "H:|-(==edgeSpacing)-[label1(==labelWidth@999)]-[label2(==label1)]-[label3(==label1)]-[label3(==label1)]-[label4(==label1)]-(==edgeSpacing)-|"
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( format : constraints,
options : .allZeros,
metrics : metrics,
views : labelsDictionary))
我只是想知道如何在 Swift 中创建间隔以确保标签之间的宽度始终相等但优先级最低。
没关系,我找到了另一种方法来实现我想要的:
override func updateConstraints() {
super.updateConstraints()
for labelName in labelsDictionary.keys {
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[\(labelName)]-|", options: .allZeros, metrics: nil, views: labelsDictionary))
}
self.addConstraint(NSLayoutConstraint(item: label1, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 0.2, constant: 0))
self.addConstraint(NSLayoutConstraint(item: label2, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 0.4, constant: 0))
self.addConstraint(NSLayoutConstraint(item: label3, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 0.6, constant: 0))
self.addConstraint(NSLayoutConstraint(item: label4, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 0.8, constant: 0))
}
您可以添加 3 个 spacer 视图,它们与 4 个标签混合。
label1-spacer1-label2-spacer2-label3-spacer3-label4
假设您在 containerView 中拥有所有 7 个视图。因为标签宽度可能会根据其文本而变化,所以它们之间的 space 相等是很好的。
self.containerView.removeConstraints(containerView.constraints())
let views = ["label1": label1,
"label2": label2,
"label3": label3,
"label4": label4,
"spacer1": spacer1,
"spacer2": spacer2,
"spacer3": spacer3
]
let metrics = ["edgeSpacing": 20, "spacingBetween": 5]
//make spacers have equal width, and greater than 5, make all views center align vertically
var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-edgeSpacing-[label1]-[spacer1(>=spacingBetween)]-[label2]-[spacer2(==spacer1)]-[label3]-[spacer3(==spacer1)]-[label4]-edgeSpacing-|", options: .AlignAllCenterY, metrics: metrics, views: views)
view.addConstraints(constH)
//configure label1 vertical alignment
let constV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[label1]-edgeSpacing-|", options: .AlignAllCenterY, metrics: metrics, views: views)
view.addConstraints(constV)
//make spacers have equal height
let constVForSpacer1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[spacer1(==spacer2)]-edgeSpacing-|", options: .allZeros, metrics: metrics, views: views)
view.addConstraints(constVForSpacer1)
let constVForSpacer2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[spacer2(==spacer3)]-edgeSpacing-|", options: .allZeros, metrics: metrics, views: views)
view.addConstraints(constVForSpacer2)
AutoLayoutbyExample via Apple Developer Documentation
有人提供了如何创建间隔视图以在 VFL 中将标签之间的间距设置为相等的示例吗?
labelsDictionary = [ "label1": label1,
"label2": label2,
"label3": label3,
"label4": label4 ]
let metrics = ["edgeSpacing": 20, "spacingBetween": 5, "labelWidth": 50]
let constraints = "H:|-(==edgeSpacing)-[label1(==labelWidth@999)]-[label2(==label1)]-[label3(==label1)]-[label3(==label1)]-[label4(==label1)]-(==edgeSpacing)-|"
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( format : constraints,
options : .allZeros,
metrics : metrics,
views : labelsDictionary))
我只是想知道如何在 Swift 中创建间隔以确保标签之间的宽度始终相等但优先级最低。
没关系,我找到了另一种方法来实现我想要的:
override func updateConstraints() {
super.updateConstraints()
for labelName in labelsDictionary.keys {
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[\(labelName)]-|", options: .allZeros, metrics: nil, views: labelsDictionary))
}
self.addConstraint(NSLayoutConstraint(item: label1, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 0.2, constant: 0))
self.addConstraint(NSLayoutConstraint(item: label2, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 0.4, constant: 0))
self.addConstraint(NSLayoutConstraint(item: label3, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 0.6, constant: 0))
self.addConstraint(NSLayoutConstraint(item: label4, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 0.8, constant: 0))
}
您可以添加 3 个 spacer 视图,它们与 4 个标签混合。
label1-spacer1-label2-spacer2-label3-spacer3-label4
假设您在 containerView 中拥有所有 7 个视图。因为标签宽度可能会根据其文本而变化,所以它们之间的 space 相等是很好的。
self.containerView.removeConstraints(containerView.constraints())
let views = ["label1": label1,
"label2": label2,
"label3": label3,
"label4": label4,
"spacer1": spacer1,
"spacer2": spacer2,
"spacer3": spacer3
]
let metrics = ["edgeSpacing": 20, "spacingBetween": 5]
//make spacers have equal width, and greater than 5, make all views center align vertically
var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-edgeSpacing-[label1]-[spacer1(>=spacingBetween)]-[label2]-[spacer2(==spacer1)]-[label3]-[spacer3(==spacer1)]-[label4]-edgeSpacing-|", options: .AlignAllCenterY, metrics: metrics, views: views)
view.addConstraints(constH)
//configure label1 vertical alignment
let constV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[label1]-edgeSpacing-|", options: .AlignAllCenterY, metrics: metrics, views: views)
view.addConstraints(constV)
//make spacers have equal height
let constVForSpacer1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[spacer1(==spacer2)]-edgeSpacing-|", options: .allZeros, metrics: metrics, views: views)
view.addConstraints(constVForSpacer1)
let constVForSpacer2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[spacer2(==spacer3)]-edgeSpacing-|", options: .allZeros, metrics: metrics, views: views)
view.addConstraints(constVForSpacer2)