0
votes

I'm trying to pin an image view to the top, left, and right of a superview, and give it a height with NSLayoutConstraint like so:

backgroundView.addSubview(imageView)
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Top, relatedBy: .Equal, toItem: backgroundView, attribute: .Top, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Left, relatedBy: .Equal, toItem: backgroundView, attribute: .Left, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Right, relatedBy: .Equal, toItem: backgroundView, attribute: .Right, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: view.bounds.width))

But the image doesn't show up and the debugger is logging that my constraints are broken. I don't see what constraint could be causing the problem.

1

1 Answers

1
votes

fixed by adding imageView.translatesAutoresizingMaskIntoConstraints = false before adding the imageView as a subview to backgroundView.