防止cell.backgroundView图像被挤压?

时间:2016-01-13 06:10:22

标签: ios uitableview

我目前正在将我的UITableViewCells的backgroundView设置为从eBay的API中提取的图像。

cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageWithData:imageData] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
[cell setNeedsLayout];

问题是表格会尝试挤压图像以适应单元格的高度,如下所示: enter image description here

我想要它只是在单元格中显示图像的中间部分,如下所示。有没有办法做到这一点?

enter image description here

1 个答案:

答案 0 :(得分:1)

当您将Imageview添加为单元格的背景视图时,必须在设置为单元格背景视图之前设置图像的内容模式:

请尝试以下代码并告知我们。

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imageData]];
    imageView.contentMode = UIViewContentModeCenter;
    imageView.clipsToBounds = YES;

    cell.backgroundView = imageView;