2013-07-19 33 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

相關問題