Gavin Miller
Gavin Miller

Reputation: 43875

UITableViewCell background color

I have a UITableViewController that I'm trying to custom style. I've got 95% of the way there, but am having trouble removing an odd shading from the cell background. I've highlighted what I'm trying to remove in this screen shot:

Normal View

To try and resolve this issue, I've dumped all the views that are composing my UITableView and then colored them to try and find the view I need to change:

Styled View

As you can see, I've been unable to color that specific spot. Said differently, I can't find a view associated with that spot.

How do I remove that shading from my cells?

Upvotes: 4

Views: 274

Answers (1)

jrturton
jrturton

Reputation: 119292

The table view is restarting the gradient behind each section, since it is just taking the colour you have given it.

Instead, use the backgroundView property of the table view - create an image view using your image, and set this as the background view, or create a UIView with background colour as you are doing above.

You will need to remove any code you have setting background colours elsewhere, and make sure your section header views either have clear backgrounds, or a contrasting background.

As a simple demonstration, I have created a grouped table view with its background colour set to clear, and the following code in viewDidLoad of the table view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *backgroundView = [[UIView alloc] initWithFrame:self.tableView.bounds];
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id)[UIColor whiteColor].CGColor, nil];
    layer.frame = backgroundView.frame;
    [backgroundView.layer addSublayer:layer];
    self.tableView.backgroundView = backgroundView;
}

This gives the following (admittedly, pretty ugly!). The cells "float" on top of this.

enter image description here

Upvotes: 3

Related Questions