I'm struggling with a "small" issue. I have a UITableView who displays images loaded from a remote server. For each one of these images I have their width and height, so I use those values to calculate the aspect ratio.
I have a height constraint on the Image View where I'm displaying the image, so when I have the aspect ratio, I change this constraint before returning the cell on the delegate's method.
In order to calculate the aspect ratio I use the width of the Image View, and this one is calculated using constraints on IB. Like this.
The width I'm getting when I'm dequeuing the cell is the same set at IB. So my aspect ratio is wrong for a moment because is not using the proper width. Also, for a brief moment, the cell is using the height constraint set on IB before changing to the height constraint set on code. This is the method I use.
- (void)updateConstraint
{
// get method has been created by me on a NSDictionary extension
NSNumber *heightNumber = [self.articleItem.data get:kHeightKey];
NSNumber *widthNumber = [self.articleItem.data get:kWidthKey];
self.imageAspectRatio = 16/9;
if (heightNumber != nil && widthNumber != nil) {
self.imageWidth = widthNumber.floatValue;
self.imageHeight = heightNumber.floatValue;
self.imageAspectRatio = self.imageWidth / self.imageHeight;
}
self.imageHeightConstraint.constant = self.articleImageView.frame.size.width / self.imageAspectRatio;
}
For cell height I'm returning UITableViewAutomaticDimension.
I don't know if I'm doing all the calculation correctly and in the proper place (obviously not). Anyone can show the wight way? Thank you so much.


layoutIfNeeded. Did you try that? - kchromik