在VFL中创建间隔物

时间:2015-01-19 04:09:08

标签: ios objective-c swift autolayout visual-format-language

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中创建间隔符以确保标签之间的宽度始终相等但优先级最低。

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以添加3个间隔视图,它们与4个标签混合。

label1的-spacer1-LABEL2-spacer2-LABEL3-spacer3-label4

假设您在containerView中拥有所有7个视图。因为标签宽度可能会根据其文本而改变,所以它们之间的空间相等是很好的。

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)

答案 1 :(得分:0)

没关系,我能够找到另一种方法来达到我想要的目的:

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))
}