I have a custom UITableViewCell where I want to display an image with a height I've set.
As it is a custom cell, I want the image to be modified by the UITable that uses this custom cell (from cellForRowAt
override).
In this case, I want to fix the height to 25.0
, but I want to keep the aspect ratio of the image.
Setting contentMode
property to .scaleAspectFit
centers my image (no idea why), and this is not I want (I want it to be on the left).
What I've done so far is setting the translatesAutoresizingMaskIntoConstraints
to false and set up constraints, like so (logo
is a UIImageView
):
// logo settings
logo.translatesAutoresizingMaskIntoConstraints = false
aView.addSubview(logo)
NSLayoutConstraint.activate([
// logo constraints
logo.topAnchor.constraint(equalTo: aView.topAnchor, constant: 30.0),
logo.leadingAnchor.constraint(equalTo: aView.leadingAnchor, constant: 40.0),
logo.heightAnchor.constraint(equalToConstant: 25.0), // fixed height
])
Then, when using the customCell:
// ... some code before
cell.logo.image = UIImage(named: "newImage")
Any idea how I can keep the aspect ratio of the image and have a fixed height to it?
EDIT:
Sorry for being so evasive, here's an example of what I have and what and I want:
I have images on a server. Those images are downloaded in my app and displayed into a custom cell, but I don't know the width nor height of them.
Let's say the app retrieves an image that is 100w and 50h: I want to display it on the cell with a fixed height of 25 points.
Naturally, I want it to have the correct width (in this case it would be 50w and 25h). I don't want it to be cropped, I just want to display it with a height of 25 points. The width is only limited to its view bounds.