2013-02-15 4 views
0

CAGradientLayer를 사용하여 내 uButton의 스타일을 지정하고 있습니다. 기본적으로 세 개의 버튼이 셀에 있습니다. 먼저 CAGradientLayer 속성을 설정할 때 잘 동작합니다 (그래디언트를 얻습니다).CALayer의 QuartzCore 관련 문제

그러나 CAGradientLayer를 두 번째 응용 프로그램에 적용하면 두 번째 응용 프로그램에 나타나지만 첫 번째 응용 프로그램에서는 사라집니다. 그리고 제가 3 분의 1로한다면, 그것은 3 분의 2에 나타나고 처음 2 분에서 사라질 것입니다.

UIButton 설정 사이에 CAGradientLayer를 출시하겠습니까?

//like button 
self.likeButton=[[UIButton alloc] initWithFrame:CGRectMake(10, 430, 80, 26)]; 
[self.likeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]]; 
[self.likeButton.titleLabel setTextAlignment:NSTextAlignmentLeft]; 
self.likeButton.tag=indexPath.row; 
[self.likeButton addTarget:self action:@selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[self.likeButton setBackgroundColor:[UIColor blackColor]]; 
[self.likeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[self.likeButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted]; 


CAGradientLayer *btnGradient=[CAGradientLayer layer]; 
btnGradient.frame=self.likeButton.bounds; 
btnGradient.colors=[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:41.0f/255.0f green:41.0f/255.0f blue:41.0f/255.0f alpha:1.0f]CGColor], nil]; 
[self.likeButton.layer insertSublayer:btnGradient atIndex:0]; 

CALayer *btnLayer=self.likeButton.layer; 
[btnLayer setMasksToBounds:YES]; 
[btnLayer setCornerRadius:5.0f]; 
[btnLayer setBorderWidth:1.0f]; 
[btnLayer setBorderColor:[[UIColor darkGrayColor]CGColor]]; 

[cell addSubview:self.likeButton]; 


//comment button 
UIButton *commentButton=[[UIButton alloc]initWithFrame:CGRectMake(110, 430, 80, 26)]; 
[commentButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]]; 
[commentButton.titleLabel setTextAlignment:NSTextAlignmentLeft]; 
[commentButton setBackgroundColor:[UIColor blackColor]]; 
[commentButton setTitle:@"Comment" forState:UIControlStateNormal]; 
[commentButton addTarget:self action:@selector(commentButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[commentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted]; 


CAGradientLayer *commentBGLayer=[CAGradientLayer layer]; 
commentBGLayer.frame=commentButton.bounds; 
commentBGLayer.colors=[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:41.0f/255.0f green:41.0f/255.0f blue:41.0f/255.0f alpha:1.0f]CGColor], nil]; 
[commentButton.layer insertSublayer:btnGradient atIndex:0]; 


CALayer *commentBLayer=commentButton.layer; 
[commentBLayer setMasksToBounds:YES]; 
[commentBLayer setCornerRadius:5.0f]; 
[commentBLayer setBorderWidth:1.0f]; 
[commentBLayer setBorderColor:[[UIColor darkGrayColor]CGColor]]; 
[cell addSubview:commentButton]; 

답변

0

당신은 복사 - 붙여 넣기 오류가있어 :

는 여기에 몇 가지 예제 코드입니다. 당신은 두 개의 서로 다른 그라데이션 레이어를 만드는,하지만 당신은 각 버튼에 동일한 하나를 추가하는 -이 라인 :

[commentButton.layer insertSublayer:btnGradient atIndex:0]; 

읽어야합니다

[commentButton.layer insertSublayer:commentBGLayer atIndex:0];