UITableViewCell with custom gradient background, with another gradient as highlight color

前端 未结 3 783
庸人自扰
庸人自扰 2020-12-08 06:29

I have a custom UITableViewCell with a custom layout. I wanted a gradient background, so in my UITableViewDelegate cellForRowAtIndexPath: method, I create a CAGradientLayer

3条回答
  •  执念已碎
    2020-12-08 06:49

    I know this thread is old, but here's a solution for the first part of your question (adding a gradient to the selected and non-selected states of a cell):

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {    
        [cell setBackgroundColor:[UIColor clearColor]];
    
        CAGradientLayer *grad = [CAGradientLayer layer];
        grad.frame = cell.bounds;
        grad.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
    
        [cell setBackgroundView:[[UIView alloc] init]];
        [cell.backgroundView.layer insertSublayer:grad atIndex:0];
    
        CAGradientLayer *selectedGrad = [CAGradientLayer layer];
        selectedGrad.frame = cell.bounds;
        selectedGrad.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
    
        [cell setSelectedBackgroundView:[[UIView alloc] init]];
        [cell.selectedBackgroundView.layer insertSublayer:selectedGrad atIndex:0];
    }
    

提交回复
热议问题