How to resize UIImageView based on UIImage’s size/ratio in Swift 3?

I spent many hours trying to find a solution to the same problem you’re having and this is the only solution that worked for me (Swift 4, Xcode 9.2):

class ScaledHeightImageView: UIImageView {

    override var intrinsicContentSize: CGSize {

        if let myImage = self.image {
            let myImageWidth = myImage.size.width
            let myImageHeight = myImage.size.height
            let myViewWidth = self.frame.size.width
 
            let ratio = myViewWidth/myImageWidth
            let scaledHeight = myImageHeight * ratio

            return CGSize(width: myViewWidth, height: scaledHeight)
        }

        return CGSize(width: -1.0, height: -1.0)
    }

}

Add the class to the project and set the UIImageView to the custom class ScaledHeightImageView. The image view’s content mode is Aspect Fit.

My problem is the same as the one stated in this post. Inside my prototype TableViewCell’s ContentView, I have a vertical StackView constrained to each edge. Inside the StackView there was a Label, ImageView and another Label. Having the ImageView set to AspectFit was not enough. The image would be the proper size and proportions but the ImageView didn’t wrap the actual image leaving a bunch of extra space between the image and label (just like in the image above). The ImageView height seemed to match height of the original image rather than the height of the resized image (after aspectFit did it’s job). Other solutions I found didn’t completely resolve the problem for various reasons. I hope this helps someone.

Leave a Comment