1

-Hello, how can I add a CAGradientLayer to a tableview cell. The table view is in grouped mode that means it can have rounded corners as well. I tried:

  CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
  gradientLayer.frame = cell.frame;

  gradientLayer.colors = [NSArray arrayWithObjects: 
                          [UIColor redColor].CGColor,
                          [UIColor blueColor].CGColor, nil];

  gradientLayer.locations = [NSArray arrayWithObjects: 
                             [NSNumber numberWithFloat: 0],
                             [NSNumber numberWithFloat: 1], nil];


  gradientLayer.masksToBounds = true;

  [cell.backgroundView.layer addSublayer: gradientLayer];

  //[cell.contentView.layer addSublayer : gradientLayer];

  [gradientLayer release];

If I set masksToBounds = true does that in any way affect the layer's corner radius?

If I add to the contentview it hides everything from the cell. If I add to the backgroundview it almost never shows, when it shows it is without rounded corners and only in the bottom part of the first cell, I have no idea why.

I add the gradient layer in my - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method. Is this the right way to do this? Will I have rounded corners without manually using some paths or something? Any help is appreciated.

gyozo kudor
  • 6,284
  • 10
  • 53
  • 80
  • does anyone knows how to add a rounded corners to the first and last cells after adding this gradient layer ? – ahmad Sep 03 '12 at 11:35

1 Answers1

1

Have a look at these pages

Community
  • 1
  • 1
Frank
  • 3,376
  • 1
  • 23
  • 23