derdida
derdida

Reputation: 14904

Programmatically create Constraints - 2 Boxes

I would like to to create the following result:

enter image description here

With only manually created NSLayoutConstraints. But at the moment i only have a blue box, or (if i do not add the UIButton) the correct red View.

    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)

Can anyone help me out whats wrong here? Is there a difference, in which order i create manually the constraints?

Upvotes: 0

Views: 59

Answers (1)

Code Different
Code Different

Reputation: 93161

Your code can be a lot more compact by using the 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 tells Auto Layout to make the red view fill the superview vertically.
  • c2 do the same horizontally
  • c3 tells Auto Layout to make the blue button fill vertically
  • c4 makes it stick to the left edge of the screen
  • c5: some constraints cannot be written with the Visual Format Language, hence we need to use a different function to make disableButton.width = newView.width * 0.75

I could have written the last line as

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

But for some reason this takes ridiculously long to compile. Hence the split you see in the code above.

Upvotes: 2

Related Questions