Reputation: 5822
I want to add constraint to the UIImageView
by adding this line of code:
addConstraint(NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": userProfileImageView]))
But xcode show me this error:
How I can fix this error?
Upvotes: 13
Views: 6613
Reputation: 4836
Use addConstraints
, instead of addConstraint
.
constraintsWithVisualFormat
returns an array.
Your code becomes:
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: [], metrics: nil, views: ["v0": userProfileImageView])
Upvotes: 36
Reputation: 36277
let horizontalprofileViewConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": userProfileImageView]
If you click option and hover over horizontalprofileViewConstraint
you will see its type as [NSLayoutConstraint]
which is already an array.
So what you can do is:
view.addConstraints(horizontalprofileViewConstraint)
if you have more than one view then you cand do:
view.addConstraints(horizontalprofileViewConstraint + verticalprofileViewConstraint)
The +
joins to arrays for you.
Upvotes: 2