Alex Rivera | Logout

UITableView section with clearColor background?

Asked 2012-10-05T17:49:57.893
8

I have a ViewController with a radial gradient background. Inside that I have a UITableView with cells and sections. Now I want to show the gradient behind the table view as you scroll. The issue is that when the section locks at the top you can see the cells behind the section. I would set the section background color but if I do that it does not match the radial gradient background. Is there anyway to have the cells clip under the sections while keeping the clearColor background?

Edit
Report

1 Answer

8

if I understand it, you want a completely clear tableview and tableview cell so you can see the background of your view controller.

You can achieve this by setting the tableview as having no background

tableview.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;

and also the cell

cell.backgroundColor = [UIColor clearColor]

if you also want the section to be transparent, you can use this method:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

to make a custom view (for example create an image view with a transparent .png for the background)

I hope I understand your question correctly.


edit after clarification

to give the appearance that the cells stop before going underneath the section header:

make a class that is a subclass of uitableview with this ivar:

@interface CustomTableView : UITableView
{
        CAGradientLayer* maskLayer;
}

obviously this will create a gradient but if you wanted you could either tweak it or use CALayer and instead of programmatically creating the mask, create a .png with the correct width/height of your section header.

ANYWAY: if you use the CAGradientLayer:

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) 
    {
        [self setupGradients]; //call your method
    }
    return self;
}

- (void)setupGradients
{
    [self addObserver:self forKeyPath:@"contentOffset" options:0 context:nil];

    */ ***** if you choose to use CALayer instead of CAGradient ****** */
    //maskLayer = [[CALayer layer] retain]; 
    //maskLayer.contents = (id) [UIImage imageNamed:@"maskLayer.png"].CGImage; 
    //maskLayer.backgroundColor = [UIColor clearColor].CGColor; 
    */ ***
answered 2012-10-05T18:01:16.887

Your Answer