Setting UIImageView image affects layout constraints

Try to lower the content compression/hugging. I think (correct me if I am wrong) the UIImageView has by default a higher compression/hugging resistance than a UIView (251 to 250). You could try the code below:

someImage.setContentHuggingPriority(UILayoutPriority(rawValue: 249), for: .horizontal)
someImage.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 249), for: .horizontal)
someImage.setContentHuggingPriority(UILayoutPriority(rawValue: 249), for: .vertical)
someImage.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 249), for: .vertical)

Is your UIImageView masking to bounds? If the image is bigger than the UIImageView then by default it will extend past the bounds

imageView.layer.masksToBounds = true

Alternatively have you tried changing the content fit of the UIImageView to Aspect Fit for example?


try to set clipsToBounds property to true for UIImageView and check, since for few contentMode options the image goes out of bound of image view if the bounds are not enough to fill the content.