左标签 - 使用Autolayout(Swift)的右标签

时间:2016-04-06 20:00:09

标签: swift autolayout

我在swift中使用autolayout,如果有更好的方法,我很好奇。

基本上我正在定位左标签和右标签。像这样......

[左标签--------------------右标签]

这就是我所做的。

let bottomView = UIView(frame : CGRectMake(0, 150, 380, 25))
bottomView.backgroundColor = UIColor.greenColor()
self.view.addSubview(bottomView)

let leftLabel = UILabel()
leftLabel.text = "Left"

let rightLabel = UILabel()
rightLabel.text = "Right Label"
rightLabel.textAlignment = NSTextAlignment.Right

let views = [
    "left" : leftLabel,
    "right" : rightLabel
]

for (key,view) in views {
    view.translatesAutoresizingMaskIntoConstraints = false
    bottomView.addSubview(view)
    // It seems like there should be better way of handling this
    self.view.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat("V:|[\(key)]|",
            options: [NSLayoutFormatOptions.AlignAllCenterX],
            metrics:nil,
            views:views
        )
    )

}

// why does the right "spacing" have to be 10 and the left only has to be 5?
self.view.addConstraints(
    NSLayoutConstraint.constraintsWithVisualFormat(
        "H:|-5-[left]-(>=0)-[right]-10-|",
        options: [NSLayoutFormatOptions.DirectionLeftToRight],
        metrics:nil,
        views:views)
)

1)我觉得有更好的方法来处理垂直部分。 2)为什么右侧间距必须是" 10"匹配左侧" 5"

0 个答案:

没有答案