Resizing UIImageView inside UITableViewCell

in my application, the size UITableViewCell

is dynamic, which means each has its own height. inside each cell there is a background image in UIImageView

. Through the storyboard, I used autolayouts

to adjust UIImageView

to automatically stretch as the cell stretches. The problem is that when they stretch the whole image, it gets stretched at the corners. So I looked online and I came up with resizableImageWithCapInsets: to stretch the stretchable sides and exclude unwanted ones. So I tried the following code in tableView: cellForRowAtIndexPath: as I only need the image to stretch vertically (height):

[cell.backgroundImage.image resizableImageWithCapInsets:UIEdgeInsetsMake(16, 0, 16, 0)];

      

however the problem continued like below picture

enter image description here

As you can see, the corners of the image are still stretched. What am I doing wrong? incorrect edgeInset values? or should I put the code somewhere else?

+1


source to share


1 answer


You create UIEdgeInsets values โ€‹โ€‹using the UIEdgeInsetsMake () function. UIEdgeInsets encapsulates four different CGFloat values โ€‹โ€‹for nested values top, bottom, left, and right

individually. Positive insertion values โ€‹โ€‹shrink the content area, while negative insertion values โ€‹โ€‹will effectively enlarge it. Make sure you create the image first and apply the insets there, and then once that's done, apply the image to the backgroundView instead of trying to edit the edge inserts from the backgroundView.image property.



     UIImageView *cellBGIV = 
    [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"imageName"]
           resizableImageWithCapInsets:UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0)]];
    cell.backgroundView = cellBGIV;

      

0


source







All Articles