0
votes

I'm trying to build a simple tableview with a tableView showing a list of images people posted and comments their friends added.

enter image description here

This is a subclass of TableViewCell, and I used stack views to lay it out. The problem is that I want to show the photos without stretching them - I want them to keep their original aspect ratio, but people can upload images of any dimensions. You see in the screenshot that the image is squished.

What I noticed is that the larger the uploaded .jpeg file, the taller the cells grew (I'm guessing the image size is dictating the intrinsic height?), but since the width can't change, each image is squished differently. A huge picture will show up super tall but with the same width.

I tried setting the image to Aspect Fit, but only the visible image itself does what I want: fit to width and not warp the aspect ratio. However, the UIImageView itself is still super tall and pushing all other UI elements out of the way.

I must be missing something here... It shouldn't be so hard, should it?

2
show the code you use to layout the cell, and code of the cellForRowAtMilan Nosáľ
I agree with Milan cell.imageView?.contentMode = .scaleAspectFitJake
@MilanNosáľ I made the interface in the storyboard, and just used this in my cellForRowAt: let cell = tableView.dequeueReusableCell(withIdentifier: "PostContentCell", for: indexPath) as! PostTableViewCell cell.initializeData(thisPost: post) return cellYou Speak So Well
then initializeData codeMilan Nosáľ

2 Answers

1
votes

To constraint the image as aspect ratio , first give it width of screen , then give it a static height in IB and hook that constraint as IBOutlet

and set it inside cellForRow

   cell.imageHeightcon.constant = imageRealHeight*imageWidthInIB/imageRealWidth
-1
votes

You should fix the size of the image view for whatever you want. Then you should use aspect to fill instead of aspect to fit and select the option clip to bounds in the attribute inspection