Ios 设置CALayer';“borderWidth”属性是否仅影响左边框和右边框?

Ios 设置CALayer';“borderWidth”属性是否仅影响左边框和右边框?,ios,uitableview,selection,quartz-core,Ios,Uitableview,Selection,Quartz Core,我正在尝试为UITableViewCell选择创建自定义颜色。我不想在按下时突出显示整个单元格,换句话说,选择背景的边框应该是(10,cell.frame.origin.y,300,cell.frame.size.height)。我尝试将backgroundColorView.layer.borderWidth属性的值设置为10,但这会影响整个视图,而不仅仅是左右边框。这就是我现在坚持的代码: UIView *backgroundColorView = [[UIView alloc] init]

我正在尝试为UITableViewCell选择创建自定义颜色。我不想在按下时突出显示整个单元格,换句话说,选择背景的边框应该是(10,cell.frame.origin.y,300,cell.frame.size.height)。我尝试将backgroundColorView.layer.borderWidth属性的值设置为10,但这会影响整个视图,而不仅仅是左右边框。这就是我现在坚持的代码:

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];

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

我认为最简单的解决方案是在视图中添加两层,如下所示:

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,10,高度)和(宽度-10,0,10,高度)。无论如何谢谢你!干杯