woutr_be
woutr_be

Reputation: 9722

Change background of a grouped UITableView

I'm having some trouble trying to change the background of a UITableView with groups.

_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableViewBg.png"]];

This usually works on every other UITableView, but not the one with groups, is there something else I have to do? In IB I have the background color set to a clear color, but that doesn't do much.

Upvotes: 21

Views: 18042

Answers (8)

ramchandra n
ramchandra n

Reputation: 2027

Swift 4.2

TO REMOVE BACKGROUND VIEW & COLOR

tableView.backgroundView = nil
tableView.backgroundColor = .clear

Upvotes: 0

Jonathan Cabrera
Jonathan Cabrera

Reputation: 1751

For iOS 9+, changing the background color of the UITableView is enough.

Objective-C

tableView.backgroundColor = [UIColor redColor];

Swift

tableView.backgroundColor = .red

Upvotes: 0

pkaur
pkaur

Reputation: 95

The selected answer works but it clears the background of both tableHeaderView and table section header view. If you just want table header to be of a certain color say white and section header still to be default grey than do the following -

tableView.tableHeaderView.inputView.backgroundColor = [UIColor whiteColor];

Upvotes: 1

勇敢的心
勇敢的心

Reputation: 341

tableView.backgroundView = nil;
tableView.backgroundColor = [UIColor colorWithPatternImage: xxx];
// tableView.backgroundColor = [UIColor redColor];  // is ok

if you set set the backgroundColor as this, when you scroll the tableView, the backgroundColor view will scroll also. so, you can: tableView.backgroundView = nil; self.view.backgroundColor = ...

Upvotes: 1

Robert
Robert

Reputation: 38213

Just want to add to Nirav's answer - it can also be done using the iOS 5 appearance proxy.

[[UITableView appearance] setBackgroundView:nil];
[[UITableView appearance] setBackgroundColor:[UIColor lightGreyColor]];

The advantage is that it is applies globally, so you can group all your UI customisations in one place. However, it will apply to all tableViews (not just grouped style).

Upvotes: 0

Nirav Jain
Nirav Jain

Reputation: 5107

You additionally need to disable the background view of _tableView:

[_tableView setBackgroundView:nil];
 _tableView.backgroundColor = [UIColor redColor];

No need to add new view to backgroundView. This is working for me in iOS6.

Upvotes: 54

Adam Flatau
Adam Flatau

Reputation: 168

What I usually do with grouped UITableViews is set the background color to clear, and the set that pattern image to the parents view.

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableBG.png"]];
self.tableView.backgroundColor = [UIColor clearColor];

Upvotes: 3

Siam
Siam

Reputation: 109

why don't you set the tableView.backgroundView? you can alloc an image view withe the specified image and pass it to the background view instead of setting the background color.

Upvotes: 5

Related Questions