以编程方式创建约束 - 2个框

时间:2016-03-19 11:28:52

标签: ios swift autolayout

我想创建以下结果:

enter image description here

仅手动创建NSLayoutConstraints。但目前我只有一个蓝色的盒子,或者(如果我不添加UIButton)正确的红色视图。

    let newView = UIView()
    newView.backgroundColor = UIColor.redColor()
    newView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(newView)

    let verticalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
    view.addConstraint(verticalConstraint)

    let topContraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
    view.addConstraint(topContraint)

    let bottomContraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
    view.addConstraint(bottomContraint)

    let widthConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
    view.addConstraint(widthConstraint)



    let disableButton = UIButton()
    disableButton.backgroundColor = UIColor.blueColor()
    disableButton.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(disableButton)

    let leadingConstraint = NSLayoutConstraint(item: disableButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
    view.addConstraint(leadingConstraint)

    let topBContraint = NSLayoutConstraint(item: disableButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
    view.addConstraint(topBContraint)

    let bottomBContraint = NSLayoutConstraint(item: disableButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
    view.addConstraint(bottomBContraint)

    let  trailingConstraint = NSLayoutConstraint(item: disableButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: newView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
    view.addConstraint(trailingConstraint)

有人能帮我解决这里的错吗?是否存在差异,我按顺序手动创建约束?

1 个答案:

答案 0 :(得分:2)

使用Visual Format Language

,您的代码可以更加紧凑
let newView = UIView()
newView.backgroundColor = UIColor.redColor()
newView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(newView)

let disableButton = UIButton()
disableButton.backgroundColor = UIColor.blueColor()
disableButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(disableButton)

let views = ["newView": newView, "disableButton": disableButton]
let c1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|[newView]|", options: [], metrics: nil, views: views)
let c2 = NSLayoutConstraint.constraintsWithVisualFormat("H:|[newView]|", options: [], metrics: nil, views: views)
let c3 = NSLayoutConstraint.constraintsWithVisualFormat("V:|[disableButton]|", options: [], metrics: nil, views: views)
let c4 = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[disableButton]", options: [], metrics: nil, views: views)
let c5 = NSLayoutConstraint(item: disableButton, attribute: .Width, relatedBy: .Equal, toItem: newView, attribute: .Width, multiplier: 0.75, constant: 0)

NSLayoutConstraint.activateConstraints(c1 + c2 + c3 + c4)
view.addConstraint(c5)
  • c1告诉自动布局使红色视图垂直填充超视图。
  • c2横向执行
  • c3告诉自动布局使蓝色按钮垂直填充
  • c4让它贴在屏幕的左边缘
  • c5:某些约束不能用Visual Format Language编写,因此我们需要使用不同的函数来制作disableButton.width = newView.width * 0.75

我本可以把最后一行写成

NSLayoutConstraint.activateConstraints(c1 + c2 + c3 + c4 + [c5])

但由于某些原因,这需要花费很长时间才能编译。因此,您在上面的代码中看到了分裂。