75

I'm trying to set the background color of a UITableViewCell to transparent. But nothing seems to work. I have some images/buttons inside the tableViewCell and I would like to make the white grouptableview background disappear to get a 'floating' effect for the images and buttons (as if they were not inside the tableview).

Any idea how this could be accomplished?

Robert
  • 1,286
  • 1
  • 17
  • 37
levi
  • 2,035
  • 3
  • 16
  • 11

13 Answers13

123

If both the other options didn't work try this

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
Ron Srebro
  • 6,663
  • 3
  • 27
  • 40
  • 14
    myTable.backgroundColor = [UIColor clearColor]; worked for me! Thanks – sjobe Nov 05 '09 at 14:21
  • 7
    This worked for me as well, although I additionally had to use: cell.backgroundColor = [UIColor clearColor]; – Mirko Froehlich Nov 10 '09 at 07:27
  • 4
    I had to use cell.textLabel.backgroundColor=[UIColor clearColor] too! – Casebash Feb 18 '10 at 01:08
  • Note: this method works when you want to also eliminate the BORDER around a UITableViewStyleGrouped styled cell. (this comment is to help google find this answer when looking for UITableViewStyleGrouped and clear.) – Paul Cezanne Jan 05 '12 at 14:13
  • Thanks! This worked for me, while `cell.backgroundView.hidden = YES` did not. – Kevin Borders Jul 27 '13 at 18:11
  • This works. But please add that you have to background color to clear color before. – karim Apr 25 '14 at 09:58
  • 1
    You don't have to create the background view. It is there by default. cell.backgroundView.background = [UIColor clearColor] is enough. You can also do self.backgroundColor = [UIColor clearColor]; , but this can be set in Interface Builder – Moose Sep 20 '15 at 18:23
45

This is actually answered in the docs of UITableViewCell. So while you do have to setup transparency on your controls, the actual cell background color must be set per below.

"Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it."

https://developer.apple.com/documentation/uikit/uitableviewcell

Cœur
  • 37,241
  • 25
  • 195
  • 267
logancautrell
  • 8,762
  • 3
  • 39
  • 50
31

Start with these articles to get the background color correctly setup:

http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/

http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/

Then try all the following lines:

[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];

There is more than just one view hiding in a UITableViewCell. This should make all of the ones in your way clear.

JOM
  • 8,139
  • 6
  • 78
  • 111
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
27
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
}
Sam Soffes
  • 14,831
  • 9
  • 76
  • 80
iomar
  • 465
  • 4
  • 4
  • 1
    If your table view has a background color and you want your cell to be transparent so you can see your table view's background color, this is the correct solution. – Sam Soffes Oct 05 '12 at 22:13
  • How is this not the ONLY answer. Works like a charm. – Zia Jun 10 '14 at 19:56
20

I have a simple solution

Objective-c version:

cell.backgroundColor = [UIColor clearColor];

Swift version:

cell.backgroundColor = UIColor.clearColor()
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • 1
    but always set it inside - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath – Sathe_Nagaraja Dec 05 '17 at 00:53
11

By default the background of your UITableViewCell is white. Doing myCell.backgroundColor = [UIColor clearColor]; will make your UITableViewCell transparent BUT you will don't feel the difference because the UITableView (the contener of your UITableViewCell) also have a white background by default.

If you wanna see your UIView background you will have to make your cell and your table background transparent :

myTableView.backgroundColor = [UIColor clearColor];
myTableCell.backgroundColor = [UIColor clearColor];

Martin Magakian

Martin Magakian
  • 3,746
  • 5
  • 37
  • 53
2

I had a problem where my custom tableviewcell backgroundimage was overlaid with a white label background, i tried everything and finally setting background to clear color in the viewWillAppear did the trick.

      - (void)viewWillAppear:(BOOL)animated
    {
[super viewWillAppear:animated];
    self.tableView.backgroundColor = [UIColor clearColor]; // if this is not here, cell background image is covered with a white rectangle :S
    }

and in the rowForCellAtIndexPath I set the background image:

 if(!cell) {
    cell = [[[UITableViewCell alloc] initWithFrame: ...
    UIImageView *cellBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbg.jpg"]];
    [cell setBackgroundView:cellBG];
    [cellBG release];
}
texmex5
  • 4,354
  • 1
  • 26
  • 28
2

If you're using storyboards, make sure to uncheck "Opaque" for your prototype UITableViewCells

Adrian Harris Crowne
  • 1,335
  • 2
  • 12
  • 11
0

There are 3 steps:

  1. cell.contentView.backgroundColor = UIColor.clear
  2. Uncheck opaque check box for table view cell in attributes inspector in Drawing section. This you can find by clicking on the table view cell prototype in storyboard and opening the attribute inspector. Scroll down and you will find it.
  3. Select Background as Clear Color from dropdown at same location as in step 2.
  4. Run your app
0

Solution for Swift 5.1

let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
cell.backgroundColor = UIColor.clear
Swee Kwang
  • 724
  • 9
  • 15
0

After pulling hair out most of my hair, this turned out to be quite simple for xCode 13 / Swift 5.1. There are 2 things that need to be changed.

In your numberOfRowsInSection, add:

tableView.backgroundColor = UIColor.clear

so it looks something like this:

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.backgroundColor = UIColor.clear
        return 40
    }

Then in your cellForRowAt, add:

cell?.backgroundColor = UIColor.clear

so it looks like:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    cell?.backgroundColor = UIColor.clear
    return cell!
}

Thats it. Couldnt believe my eyes when it worked. :)

0

I'm late to the party, but will share my findings as they differ from all of the answers here. After spending 2 hours trying out different solutions, I managed to achieve table cell's transparency solely by tweaking two properties in the Interface Builder:

  1. Set the table view background (close to the bottom of the Attributes inspector, in the View section) to Clear Color,
  2. Set the Table View Cell background (again, in the View section) to Clear Color.

I didn't have to change the code, and the additional advantage for those still using the Interface Builder is that the table cell is displayed with transparent (not white) background, which is important when the cell's content items' color is white:

The result

javaxian
  • 1,815
  • 1
  • 21
  • 26
-1

Here is my solution in Swift 4.1:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.backgroundView = UIImageView(image: "Image")
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    cell.backgroundColor = UIColor.clear
{
Erik Olson
  • 383
  • 1
  • 3
  • 11