2013-07-19 35 views
0

我想为UITableViewCell选择做一个自定义颜色。我不希望整个单元格按下时突出显示,换句话说,选择背景的框架应该是(10,cell.frame.origin.y,300,cell.frame.size.height)。我试图给backgroundColorView.layer.borderWidth属性的值为10,但这会影响整个视图,而不仅仅是左右边框。这是我现在卡在的代码:设置CALayer的borderWidth属性只影响左右边界?

UIView *backgroundColorView = [[UIView alloc] init]; 
backgroundColorView.backgroundColor = SWITCH_COLOR_ON; 
backgroundColorView.layer.masksToBounds = YES; 
// backgroundColorView.layer.borderWidth = 10.0f; // this shrinks the entire view 
[cell setSelectedBackgroundView:backgroundColorView]; 

有关如何使这项工作的任何提示?谢谢。

回答

1

我认为最简单的解决方案是将2层添加到这样的观点:

CALayer *leftBorder = [CALayer layer]; 
[leftBorder setBackgroundColor:[[UIColor blackColor] CGColor]]; 
[leftBorder setFrame:CGRectMake(0, 0, 5, yourView.frame.size.height)]; 
[yourView.layer addSublayer:leftBorder]; 

CALayer *rightBorder = [CALayer layer]; 
[rightBorder setBackgroundColor:[[UIColor blackColor] CGColor]]; 
[rightBorder setFrame:CGRectMake(yourView.frame.size.width-5, 0, 5, yourView.frame.size.height)]; 
[yourView.layer addSublayer:rightBorder]; 

此代码添加5个像素宽度和黑色颜色的两个黑色边框。希望能帮助到你。

+0

工作得很好,谢谢!我要提到的唯一的事情是它必须是*(0,0,10,高度)和(宽度-10,0,10,高度)。不管怎么说,多谢拉!干杯 – nemesis

相关问题