2014-04-01 55 views
0

我想在UITableViewCell的背面设置拉伸背景图片。 这是一个常见的问题,回答了很多次,所以我在做它的“标准方式”:UITableViewCell backgroundView不拉伸

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 
    UIImage* bgImg; 

    bgImg = [[UIImage imageNamed:@"menu_bg_selected.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 1, 0)]; //top/left/bottom/right 
    cell = [tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath]; 
    [cell setBackgroundView:[[UIImageView alloc] initWithImage:bgImg]]; 
    return cell; 
} 

如果我只是试试这个,我得到一个平铺的背景。 阅读几个类似的问题/答案后,我曾尝试:

  • 强制后台查看的规模:背景视图cell.backgroundView.contentMode = UIViewContentModeScaleToFill;

  • 力大小调整:cell.backgroundView.clipsToBounds = YES;

  • 面具的设置自动调整大小:cell.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

  • 试图为力的背景的帧大小UIImageView

似乎我的背景图像被部分地拉伸到预定尺寸,然后平铺(没有足够的声誉尚未上传图片,遗憾。 ..):

  • 垂直拉伸是局部的和不使用全高
  • 横向拉伸是局部的和不使用全宽度

背景图片是一张6x3像素的图片(左侧蓝色垂直线,深灰色背面和底部浅灰色水平线)。

我在做什么错?

感谢您的帮助!

[编辑] 实际上这个问题似乎并不是一个UIEdgeInsetsMake问题。 当使用bgImg = [UIImage imageNamed:@“menu_bg_selected.png”];背景图片仍然没有填充完整的单元格背景。

+0

cell.backgroundView.contentMode = UIViewContentModeTop; –

+0

好吧我知道了,我需要使用resizingMode参数:'bgImg = [[UIImage imageNamed:@“menu_bg_selected.png”] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5,1,0)resizingMode:UIImageResizingModeStretch];' – Noth

+0

I'尽快回答我自己的问题(尽量不要做)。感谢大家的建议! – Noth

回答

0

我相信这个问题是来自

UIEdgeInsetsMake(0, 5, 1, 0) 

基本上你说我希望有一个伸展的图像,但我不想底线伸展既不左边框。这没有任何意义。你的边缘插图必须留下一些可拉伸的部分。举例来说,如果你的形象是6X3你可以有:

UIEdgeInsetsMake(1, 2, 1, 2) 

但是你可能想之前重新设计你的形象。

+0

事实上,我认为这意味着“不要在左侧拉伸5个像素,在底部拉伸像素”底部和左侧的“不可拉伸”区域将在平铺其余的背景时被平铺。我错了吗?无论如何,我尝试了你的建议:局部垂直拉伸失去了,图像完全平铺,没有任何拉伸。 – Noth

+0

我相信你错了,使用这个功能,图片应该伸展,而不是平铺。如果你想伸展,我相信你必须留下一个可拉伸的部分(意思是没有任何上限)你用我刚刚编辑的代码尝试过吗? – streem

+0

是的,只是试过!它不能解决问题,背景图像拉伸方式不同,但仍然可以填充完整的单元背景... – Noth

0

此代码适用于UIImage

UIIMage *bgImage = [[UIImage imageNamed:@"menu_bg_selected.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15]; 
+0

它似乎并没有解决它。我的背景图片是6x3像素,所以左上边距15似乎很明显。 – Noth

+0

确定您更改了保证金并检查了 – Dipin

+0

这不是Cap值的问题。顺便说一下,自iOS 5.0以来,不再推荐使用stretchableImageWithLeftCapWidth – Noth

0

好吧,我明白了!

问题是我的背景图片只是部分拉伸,然后平铺,以填充完整的单元格(无论UIEdgeInsetsMake参数是什么)。

我成功地使用了:resizableImageWithCapInsets和指定resizingMode参数

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 

    UIImage* bgImg; 
    bgImg = [[UIImage imageNamed:@"menu_bg_selected.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 1, 0) resizingMode:UIImageResizingModeStretch]; 
    cell = [tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath]; 
    [cell setBackgroundView:[[UIImageView alloc] initWithImage:bgImg]]; 
    return cell; 
} 

感谢大家的建议!