How to create a UITableViewCell with a transparent background

IphoneCocoa TouchUitableview

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

If both the other options didn't work try this

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;

Solution 2 - Iphone

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

Solution 3 - Iphone

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.

Solution 4 - Iphone

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
}

Solution 5 - Iphone

I have a simple solution

Objective-c version:

cell.backgroundColor = [UIColor clearColor];

Swift version:

cell.backgroundColor = UIColor.clearColor()

Solution 6 - Iphone

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

Solution 7 - Iphone

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];
}

Solution 8 - Iphone

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

Solution 9 - Iphone

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

Solution 10 - Iphone

Solution for Swift 5.1

let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
cell.backgroundColor = UIColor.clear

Solution 11 - Iphone

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. :)

Solution 12 - Iphone

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

Solution 13 - Iphone

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
{

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleviView Question on Stackoverflow
Solution 1 - IphoneRon SrebroView Answer on Stackoverflow
Solution 2 - IphonelogancautrellView Answer on Stackoverflow
Solution 3 - IphoneCorey FloydView Answer on Stackoverflow
Solution 4 - IphoneiomarView Answer on Stackoverflow
Solution 5 - IphoneMuhammad Aamir AliView Answer on Stackoverflow
Solution 6 - IphoneMartin MagakianView Answer on Stackoverflow
Solution 7 - Iphonetexmex5View Answer on Stackoverflow
Solution 8 - IphoneAdrian Harris CrowneView Answer on Stackoverflow
Solution 9 - IphoneEhtesham SiddiquieView Answer on Stackoverflow
Solution 10 - IphoneSwee KwangView Answer on Stackoverflow
Solution 11 - IphoneAdrian WarrinerView Answer on Stackoverflow
Solution 12 - IphonejavaxianView Answer on Stackoverflow
Solution 13 - IphoneErik OlsonView Answer on Stackoverflow