2012-07-31 32 views
0

我試圖做一個自定義的UIButton類,除,繪製按鈕的背景時,添加它作爲使用insertSubLayer後面的子層:方法,它仍會出現盈方的UIButton的爲textLabel。CAGradientLayer出現上述

我的代碼貼在下面,任何幫助將不勝感激。

CALayer *layer = self.layer; 

layer.cornerRadius = 3.0f; 
layer.masksToBounds = YES; 
layer.borderWidth = 1.0f; 
layer.borderColor = [UIColor colorWithWhite:0.5f alpha:0.5f].CGColor; 
self.titleLabel.textColor = [UIColor greenColor]; 
//layer.backgroundColor = [UIColor greenColor].CGColor; 

bgColor = [CAGradientLayer layer]; 
bgColor.frame = self.layer.bounds; 
self.backgroundColor = [UIColor colorWithWhite:1 alpha:1]; 
bgColor.colors = [NSArray arrayWithObjects: 
        (id)[UIColor colorWithWhite:0.97f alpha:1].CGColor, 
        (id)[UIColor colorWithWhite:0.87f alpha:1].CGColor, 
        nil]; 
bgColor.locations = [NSArray arrayWithObjects: 
         [NSNumber numberWithFloat:0.0f], 
         [NSNumber numberWithFloat:1], 
         nil]; 
[self.layer addSublayer:bgColor]; 
[self.layer insertSublayer:bgColor below:layer]; 
+0

這是哪裏的代碼在你的按鈕類? – jrturton 2012-07-31 11:00:08

+0

它在InitWithFrame上運行 – nickw444 2012-07-31 11:02:50

回答

1

self.layerlayer在你的代碼指向同一個對象。你要求圖層在其後面插入一個子圖層 - 這是不可能的。子圖層包含在父圖層中。嘗試

[self.layer insertSublayer:bgColor atIndex:0]; 

而不是

[self.layer addSublayer:bgColor]; 
[self.layer insertSublayer:bgColor below:layer]; 

這將在可能的最低點在你的按鈕層層次結構添加漸變。

+0

你是一個救生員。工作先走。非常感謝你。讓我非常頭痛。 – nickw444 2012-07-31 11:02:22