I'm trying to achieve something similar to the 9gag app where the cells automatically resize based in the size of the image. The width hugs the sides but the height is resized as needed.
I have a Label that consists of the title above it as well as a UIImageView and below the image I have share icons etc. I've set the constraints for the image to be the UIImageView and to the lower share icons as well as to both leading and trailing spaces. It seems like I've tried every combination of switching "Mode" of the view (i.e. aspect fill, aspect fit) when I do aspect fit it sizes the images correctly but adds a bunch of padding to both the lower and upper parts of the image (screenshot included below)
The label and icons all have the appropriate constraints set (as far as I can tell)
I've searched for hours trying to find someone with a similar issue but to no avail I can't seem to find anything in regards to self sizing cells that use images that may vary in size.
Can anyone point me in the right direction or know of the solution to my problem? All help is very much appreciated as always!
As fas as I've seen, you can't really do image resizing from Interface Builder. You would have to manually write the code in your ViewController.m file. Have something like this:
- (void) adjustImageSize{
CGSize imageSize = self.imageView.image.size;
CGSize imageViewSize = self.imageView.frame.size;
float imageAspectRatio = imageSize.width/imageSize.height;
CGRect frame = self.statusImage.frame;
frame.size.height =imageViewSize.width/imageAspectRatio;
self.imageView.frame = frame;
// set your Cell's frame here, based on your other constraints (labels/buttons etc), after you have resized your image
}
You can call this from your tableView datasource function. And then add this delegate function to tell the tableView the dynamic height of each cell.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
This is how I did dynamic resizing of images for a NewsFeed-based app. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With