0

I'm using a CAGradientLayer as the background for my button.

cell.showOnMap.clipsToBounds = YES;
cell.showOnMap.layer.cornerRadius = 10.0f;
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.showOnMap.frame.size.width, cell.showOnMap.frame.size.height)];
CAGradientLayer *gradient2 = [CAGradientLayer layer];
gradient2.frame = view2.bounds;
gradient2.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.00 green:0.66 blue:0.71 alpha:1.0]  CGColor], (id)[[UIColor colorWithRed:0.18 green:0.27 blue:0.75 alpha:1.0] CGColor], nil];
[cell.showOnMap.layer insertSublayer:gradient2 atIndex:0];
[cell.showOnMap bringSubviewToFront:cell.showOnMap.imageView];

There are cases in which my button, showOnMap will be disabled. In this case, I would like the CAGradient layer to change from lightGrayColor to grayColor OR remove the layer altogether. Here's the enable/disable code.

if(entry.address == nil)
{   [cell.showOnMap setEnabled:NO];
    cell.showOnMap.layer.sublayers = nil;
}
else
    [cell.showOnMap setEnabled:YES];

So far I have tried putting the entire gradient code within the else portion, then placing the same code in the if(entry.address == nil) but with gray colors as the gradients. This didn't work; the button was always the initial blue gradient.

I also tried cell.showOnMap.layer.sublayers = nil; but this removes the text and button image I have, leaving only a background with rounded edges.

[[cell.showOnMap.layer.sublayers objectAtIndex:0] removeFromSuperlayer]; didn't produce any change and [[cell.showOnMap.layer.sublayers objectAtIndex:0] removeFromSuperview]; caused a crash

How can I reference my CAgradient layer and remove it when my button is disabled?

Eichhörnchen
  • 592
  • 2
  • 12
  • 27

1 Answers1

2

Create a gradientLayer property inside your cell. Then init your gradientLayer to this property. And then you can safely reference it later to either modify it or removeFromSuperlayer.

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
  • I changed my text to this: `UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.showOnMap.frame.size.width, cell.showOnMap.frame.size.height)]; cell.gradientBackground.frame = view2.bounds; cell.gradientBackground.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.00 green:0.66 blue:0.71 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:0.18 green:0.27 blue:0.75 alpha:1.0] CGColor], nil]; [cell.showOnMap.layer insertSublayer:cell.gradientBackground atIndex:0];` with a reference to the property in my cell's subclass, but now the gradient doesn't display – Eichhörnchen Jul 16 '14 at 00:02
  • any suggestions? do i need to declare it again as a CAGradientLayer within the method? – Eichhörnchen Jul 16 '14 at 00:03
  • 1
    You need to init the gradient into the property like this: `cell.gradientBackground = [CAGradientLayer layer]`. Also make sure the property is strong (the default). – Ricardo Sanchez-Saez Jul 16 '14 at 00:05
  • Oh! removed that line on accident. So in the method where I would disable to cell I wrote `[cell.gradientBackground removeFromSuperlayer];`, this doesn't seem to work either... – Eichhörnchen Jul 16 '14 at 00:12
  • I also tried putting the opacity to 0.0, but that didn't work. This method is in `cellForRowAtIndexPath` – Eichhörnchen Jul 16 '14 at 00:14
  • In which method are you adding the gradientLayer to the cell subview? It might be the case that you are adding several of them as cells get reused. Try calling `[cell.gradientBackground removeFromSuperlayer]` in the cell's `- (void)prepareForReuse` method. Also, it might be slightly cleaner to store the gradient property on `showOnMap` instead of the cell. – Ricardo Sanchez-Saez Jul 16 '14 at 00:21
  • I am adding the gradient in `cellForRowAtIndexPath`. The button is successfully being disabled with this method, but the gradient layer is not being removed. No luck with the `prepareForReuse` either. I just tried: `- (void)prepareForReuse { if(self.masterCell.showOnMap.enabled == NO) { [self.masterCell.gradientBackground removeFromSuperlayer]; [self.masterCell.gradientBackground setOpacity:0.0]; } }` – Eichhörnchen Jul 16 '14 at 00:27
  • Can you post the complete code for the table view controller and cell on GitHub? I'll have a look. – Ricardo Sanchez-Saez Jul 16 '14 at 08:15
  • Hey thanks, there was just something strange with the layer - I was able to achieve the look I needed by setting `[cell.showOnMap.layer setOpacity:0.6];`. Just wasn't able to set the layer's opacity. If I look at it more later I'll definitely post here... – Eichhörnchen Jul 16 '14 at 16:13