在单元格的imageView中为图像添加边框

时间:2013-08-16 13:32:05

标签: ios uitableview

我想通过imageView和imageView图层为TVC单元格的图像添加边框

我已经有了适用于cell.layer本身的代码,但是只要我将其更改为cell.imageView.layer,代码就什么都不做。

我在tableView中使用它:cellForRowAtIndexPath和我在Xcode 4.6.3,iOS 6.x

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.imageView.image = [UIImage imageNamed:@"myImage.png"];

CALayer* layer;
//layer = cell.layer;
layer = cell.imageView.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius:10.0f];
[layer setBorderWidth:10.0f];

这适用于cell.layer,但对单元格的imageView.layer完全没有任何作用。

有什么想法吗?

感谢。

编辑:仅供参考,我正在导入<QuartzCore/QuartzCore.h>

编辑:虽然这对细胞很有效,但这是因为细胞已经有一个定义为黑色的边框颜色。 imageView没有,因此需要设置边框颜色,如下所示:

[cell.imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];  
[cell.imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];  
[cell.imageView.layer setBorderColor: [[UIColor colorWithRed:.5f green:.6f blue:.3f alpha:.75f] CGColor ]];

2 个答案:

答案 0 :(得分:4)

另一种选择是,添加

#import <QuartzCore/QuartzCore.h>

FrameWork并使用

cell.imageView.layer.borderWidth = 1.0f;
cell.imageView.layer.setMasksToBounds = YES;

答案 1 :(得分:1)

虽然这个代码可以很好地对抗单元格,但这是因为单元格已经有一个定义为黑色的边框颜色。 imageView没有,因此需要设置边框颜色,如下所示:

[cell.imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];  
[cell.imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];  
[cell.imageView.layer setBorderColor: [[UIColor colorWithRed:.5f green:.6f blue:.3f alpha:.75f] CGColor ]]; 
相关问题