Resize UIImageView after scaling Aspect Fit

I have UITableViewCell, containing only UIImageView, which is tied to the supervisor on all four sides, so the cell is scaled to fit the full image.

This setting is UIImageViewset to Scale Aspect Fit so that the size of large images changes according to the width of the cell. However, after compliance with the aspect, the size UIImageViewremains the same size as before scaling, and the cell, in turn, saves the size of a larger, unscaled image.

Is there a way in Interface Builder to make the height UIImageViewdecrease when the image is reduced using the Fit property? Or, equally, is there a way to programmatically say UIImageViewto resize its height to fit the new scaled height UIImage?

+4
source share
1 answer

I encountered a similar problem when embedding a UIImageView in a UIStackView. I consider this a mistake, because when the image has the mode: AspectFit, then the unrelated dimension is expected to change to a scaled image instead of the original.

My workaround:

  • created height constraint with identifier for representing the image, for example. "ConstraintImageHeight"
  • reset , :

    imgView.image = picture; for c in imgView.constraints { if c.identifier == "constraintImageHeight" { c.constant = picture.size.height * imgView.bounds.width / picture.size.width; break; } }

+3

All Articles