0

I am using CAGradientLayer to style my uibuttons. Basically have three buttons in a cell. When I set CAGradientLayer properties on first, it works fine (I get a gradient).

However, when I apply a CAGradientLayer to the second one, it does appear on the second one but disappears from the first one. And if I do it on a third, then it will appear on the third and disappear from the first two.

Am I supposed to be releasing the CAGradientLayer in between UIButton setups?

Here's some sample code:

 //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];
Live2Enjoy7
  • 1,075
  • 2
  • 11
  • 22

1 Answers1

0

You've got a copy-paste error. You're creating two different gradient layers, but you're adding the same one to each button - this line:

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

Should read:

[commentButton.layer insertSublayer:commentBGLayer atIndex:0];
jrturton
  • 118,105
  • 32
  • 252
  • 268