在 UITableViewCell 中调整 UIImageView 的大小 [英] Resizing UIImageView inside a UITableViewCell

查看:66
本文介绍了在 UITableViewCell 中调整 UIImageView 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,UITableViewCell 大小是动态的,这意味着每个都有自己的高度.在每个单元格内的 UIImageView 中有一个背景图像.通过故事板,我使用 autolayouts 自定义 UIImageView 以在单元格拉伸时自动拉伸.问题在于,当他们拉伸整个图像时,它的边角会被拉伸.所以我在网上查找,我使用 resizableImageWithCapInsets: 来拉伸可拉伸的边并排除不需要的边.所以我在 tableView: cellForRowAtIndexPath: 中尝试了以下代码,因为我希望图像仅垂直拉伸(高度):

in my app the UITableViewCell size are dynamic which means each has it's own height. inside of each cell there is a background image in a UIImageView. Through storyboard, i used autolayouts to customize the UIImageView to automatically stretch as the cell is stretched. The problem is that when they stretch the whole image stretches with the corners. So i was looking up online and i came over using resizableImageWithCapInsets: in order to stretch the stretchable sides and exclude the unwanted ones. So i tried the following code in tableView: cellForRowAtIndexPath: as i wanted the image to be stretched vertically only (height):

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

但是问题依旧,如下图

如您所见,图像边角仍然被拉伸.我究竟做错了什么?edgeInset 值错了吗?还是应该将代码放在其他地方?

As you can see, the image corners are still stretched. What am i doing wrong? is the edgeInset values wrong? or should i place the code somewhere else ?

推荐答案

您可以使用 UIEdgeInsetsMake() 函数创建 UIEdgeInsets 值.UIEdgeInsets分别为top、bottom、left、right的inset值封装了四种不同的CGFloat值.正插入值缩小内容区域,而负插入值将有效地增加它.确保首先创建图像并在那里应用插图,完成后将图像应用到 backgroundView,而不是尝试从 backgroundView.image 属性编辑边缘插图.

You create UIEdgeInsets values using the UIEdgeInsetsMake() function. UIEdgeInsets encapsulates four different CGFloat values for the inset values of the top, bottom, left, and right individually. Positive inset values shrink the content area, while negative inset values will effectively increase it. Make sure you create the image first and apply the insets there and then, Once that is done then apply the image to the backgroundView as opposed to trying to edit the edge insets 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;

这篇关于在 UITableViewCell 中调整 UIImageView 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆