Reputation: 8497
I am trying to add a gradient to the background of a custom cell, and when I display the gradient using addSublayer it shows over the rest of the content, and when I use insertSublayer: atIndex: 0 it doesn't appear. This cell is a custom cell, what should I do?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor yellowColor]CGColor], (id)[[UIColor whiteColor]CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:1];
return cell;
}
Upvotes: 0
Views: 824
Reputation: 2575
Insert the following code. Remember that sublayers are listed from back to front order:
cell.backgroundColor = [UIColor clearColor];
[cell.layer insertSublayer:gradient atIndex:0];
Upvotes: 2
Reputation: 5881
UITableViewCell has a default background color of White. Set it to clear and you should be able to insert your gradient at index 0 and have it display.
Upvotes: -1