92

I realize that iOS 7 has not officially been released and we should not discuss it BUT I am going crazy trying to figure out this problem. On iOS 6, my table view was transparent and looked great. First time running iOS 7, and the background is white.

I have tried making the table backgroundColor, cell color etc etc to UIColor clearColor but have no change.

How to fix this problem?

the table

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Teddy13
  • 3,824
  • 11
  • 42
  • 69
  • 1
    Tried setting the table's `backgroundView` to a clear one? – Undo Sep 12 '13 at 00:25
  • Could you show the code where you are setting the background colour? What about the parent view? Perhaps the table view background is clear but the parent isn't? – sooper Sep 12 '13 at 00:33
  • I noticed this same thing but when I installed the same app that predated my current version to iOS 7.0 it wasn't happening. Does this problem lie in xCode5.0 then? – Totumus Maximus Sep 23 '13 at 13:52

21 Answers21

125
    // Fix for iOS 7 to clear backgroundColor
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView = [[UIView new] autorelease];
    cell.selectedBackgroundView = [[UIView new] autorelease];

in cellForRowAtIndexPath

Also, make sure that your tableview actually has transparent background (in storyboard): enter image description here

Alex Stone
  • 46,408
  • 55
  • 231
  • 407
slamor
  • 3,355
  • 2
  • 14
  • 13
  • George, add that code inside the method - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath – IanStallings Sep 25 '13 at 13:33
  • 1
    For the cases, if you really need selectedBackgroundView, you can put the following in cellForRowAtIndexPath: cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourBgImage.png"]] autorelease]; or even in awakeFromNib: self.selectedBackgroundView = ... Note, that: self.backgroundColor = [UIColor clearColor]; does not work in awakeFromNib under iOS7. But: cell.backgroundColor = [UIColor clearColor]; works properly only in cellForRowAtIndexPath for iOS 7.0 – slamor Sep 25 '13 at 14:50
  • 11
    Only need to add the row: cell.backgroundColor = [UIColor clearColor]; – evya Sep 30 '13 at 16:57
  • This works like charm cell.backgroundColor = [UIColor clearColor]; – bpolat Nov 16 '13 at 18:11
  • How to do this in Swift? – Qadir Hussain Aug 12 '15 at 13:28
  • This doesn't seem to work on iOS 12. I just spent a good hour trying to figure out what happened only to find my code working fine on iOS 11. No idea what the problem is. View debugging leads me nowhere except to the backgroundColor property of the TableView not reacting to my changes. – nickdnk Nov 05 '18 at 20:19
  • In Swift... tableView.backgroundColor = nil. In the cellForRowAt method, dequeue your cell and set cell.backgroundColor = nil before returning it. I just confirmed this is working in iOS 13. – PatPatchPatrick Sep 16 '20 at 02:33
58

Put this:

cell.backgroundColor = [UIColor clearColor];

In this section:

cellForRowAtIndexPath
Arun
  • 3,406
  • 4
  • 30
  • 55
  • 1
    In iOS 6, it is sufficient to set the `backgroundView` to an empty view, but this additional code is needed for iOS 7. – Kevin Borders Oct 04 '13 at 16:27
25

Try setting backgroundView to nil first.

[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];

Not sure if this is a change in documentation with iOS7 or has always been there and just did not affect background color, but per UITableView Class Reference @property backgroundView

"You must set this property to nil to set the background color of the table view."

edit: corrected code syntax

BluGeni
  • 3,378
  • 8
  • 36
  • 64
dandan
  • 391
  • 2
  • 5
25

This has been answered, but incorrectly in a lot of ways.

You need to implement the below delegate method:

    - (void)tableView:(UITableView *)tableView  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}

You can't put the fix in cellForRowAtIndexPath because that is after the cell is rendered, and it will flash a white background before the background gets set to clear (on slower devices).

Use this delegate method and your problems are solved!

edhnb
  • 2,122
  • 3
  • 24
  • 38
  • 1
    Yes. For iOS 7 this is the way to clear the background. All the other suggestions above might not work. – girish_vr Feb 26 '14 at 12:31
15

Actually the officially correct place to change cell background color is different according to documentation (UITableViewCell Class Reference):

Whether you use a predefined or custom cell, you can change the cell’s background using the backgroundView property or by changing the inherited backgroundColor property. In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.

asdusd
  • 151
  • 3
  • I am trying this, but the very first cells shown all have a white background. If I create new cells by scrolling, then they will have the transparent background I want. Not sure what else to try… – David Dunham Oct 01 '13 at 20:02
  • Never mind, it appears to be a timing issue. I was doing stuff in awakeFromNib, which was too early. – David Dunham Oct 01 '13 at 20:28
9

swift 3, 4 and 5

cell.backgroundColor = UIColor.clear
Jamal alaayq
  • 146
  • 1
  • 5
6

This is quite a frustrating problem. Here's my current solution:

Add this to your UITableViewCell subclass.

- (void)didMoveToSuperview {
    [super didMoveToSuperview];

    self.backgroundColor = [UIColor clearColor];
}
Berk
  • 367
  • 2
  • 7
5

This worked for me in iOS7+:

self.tableView.backgroundColor =[UIColor blueColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;`

and then in cellForRowAtIndexPath:

cell.backgroundColor = [UIColor clearColor];
DevC
  • 6,982
  • 9
  • 45
  • 80
5

This did only work for me when i edited a clear background color for each cell and a clear color for the table itself.. BOTH PROGRAMMATICALLY

to set the clear color for the table:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    initMenu()
    myTableView.backgroundColor = UIColor.clearColor()
}

to set the color for the cells:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tablecellid", forIndexPath: indexPath)
    cell.backgroundColor = UIColor.clearColor()
    return cell
}
SoliQuiD
  • 2,093
  • 1
  • 25
  • 29
4

try this code snippet

cell.contentView.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
Stornu2
  • 2,284
  • 3
  • 27
  • 47
4

First set

tableView.backgroundColor = [UIColor clearColor];

Second set

tableCell.backgroundColor = [UIColor clearColor];
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
Fantasyczl
  • 61
  • 4
3

One thing to nice. The default color of UITable seems to be white (I don't know why)

Background white

But better change that.

Paweł Brewczynski
  • 2,665
  • 3
  • 30
  • 43
3

create IB Outlet for table view @IBOutlet weak var yourTable : UITableView!

in view load

override func viewDidLoad() {
    yourTable.delegate = self
    yourTable.dataSource = self

    yourTable.backgroundColor = UIColor.clearColor()
}

if you want to clear color of Cell also do this in

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    cell.backgroundColor = UIColor.clearColor() 

}
2

In my app, I had to set the backgroundColor on my UITableViewCell class to [UIColor clearColor] color when I updated for iOS 7.

Arun
  • 3,406
  • 4
  • 30
  • 55
2

In my case the cell was created using a xib. it seems like interface builder on xcode5 has trouble setting clearColor to the cell.backgroundColor.

All I needed to do was indeed to set

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// get the cell from the nib
//then force the backgroundColor
cell.backgroundColor = [UIColor clearColor]
return cell;
}
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
Ben G
  • 3,965
  • 3
  • 30
  • 34
2

Try

[myTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[myTable setSeparatorInset:UIEdgeInsetsZero];

And

cell.backgroundColor = [UIColor clearColor];
Jorgy
  • 21
  • 1
2

Just select Clear Color on View Background for the table and for the cell also.

2

swift 3

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.backgroundColor = UIColor.clear
}
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
1
// Fix iOS 7 clear backgroundColor compatibility 
// I Think this two lines only are enough

cell.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = [[UIView new] autorelease];
Kiran Jasvanee
  • 6,362
  • 1
  • 36
  • 52
1

In swift 3

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath)
    cell.backgroundColor = .clear
    cell.backgroundView = UIView()
    cell.selectedBackgroundView = UIView()
    return cell
}
Patricio Bravo
  • 406
  • 6
  • 8
0

Set

tableView.backgroundColor = [UIColor clearColor];

in viewDidLoad.

if this is not working, try:

tableView.backgroundView = nil;
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84