Reputation: 171
//MARK: NSLayoutConstraints
//trailing
let trailingConstaint = NSLayoutConstraint(item: imageView, attribute: .trailing, relatedBy: .equal, toItem:imageView.superview , attribute: .trailing, multiplier: 1, constant: 0)
trailingConstaint.isActive = true
//leading
let leadingConstraint = NSLayoutConstraint(item: imageView, attribute: .leading, relatedBy: .equal, toItem: imageView.superview, attribute: .leading, multiplier: 1, constant: 0)
leadingConstraint.isActive = true
//top
let topConstraint = NSLayoutConstraint(item: image, attribute: .top, relatedBy: .equal, toItem: imageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true
//bottom
let bottomConstraint = NSLayoutConstraint(item: imageView, attribute: .bottom, relatedBy: .equal, toItem: imageView.superview, attribute: .bottom, multiplier: 1, constant: 0)
bottomConstraint.isActive = true
I wanted to resize the image so that it fits so that the image is scaled properly screenshot of ViewController
But i get this error..
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSLayoutConstraint for size {746, 496} orientation 0 scale 1.000000: Constraint items must each be an instance of UIView, or UILayoutGuide.'
Upvotes: 2
Views: 3645
Reputation: 1367
//top
let topConstraint = NSLayoutConstraint(item: image, attribute: .top, relatedBy: .equal, toItem: imageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true
You are setting this constraint to the image instead of imageView.
The exception clearly states that Constraint items must each be an instance of UIView, or UILayoutGuide.'
All the other constraints are set between imageView and imageView.superView, so logically the above constraint should also use the same pair.
This will take care of the exception.
//top
let topConstraint = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: imageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true
But, Image scaling cannot be achieved by constraints. You have to set the contentMode property appropriately. Check Understanding How Images Are Scaled section in the well-explained docs.
Upvotes: 5
Reputation: 114773
In your third constraint you have used image
instead of imageView
. You can't set constraints on an image, only on views. Look at how you are setting imageView
.
Is there a reason why you aren't simply setting the constraints in Interface Builder? They are quite straight-forward.
You can set the contentMode
property of the UIImageView
to determine how the image is scaled. scaleAspectFill
is typically what you want, although this will result in some cropping of the image if it doesn't precisely fit the view. scaleAspectFit
will ensure that the entire image is shown, but some padding will be required if the image size doesn't match the view size. scaleToFill
will fill the view without padding, potentially at the cost of distorting the image.
Upvotes: 0
Reputation: 850
It looks like you have referenced a image instead of imageView in your topConstraint. It’s hard to tell for sure without seeing a bit more of your code but try updating that and see if it works.
let topConstraint = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: imageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true
Upvotes: 1