UITableView backgroundColor always white on iPad

19.3k views Asked by At

I'm working on a project. I have plenty of UITableViews which are set as clear color. Their views' background color are set to my custom color and everything is fine on iPhone.

The issue comes up on iPad! I tried almost everything, but my UITableView has a white color.

I checked the other topics, like: UITableView backgroundColor always gray on iPad, but nothing worked. Also, my problem is not grey, it's white as snow!

What might be the reason of it?

16

There are 16 answers

7
Ben Flynn On BEST ANSWER

Good News: According to the release notes, for iOS 10:

When running on iPad, the background color set for a UITableViewCell in a Storyboard is now respected.

For versions <10:

I was seeing this in iOS 8 (8.3). Even though in IB my cells were "clear color" and their content views were "clear color" they would render as white. An imperfect but reasonable solution, since it still takes values from IB:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    cell.backgroundColor = cell.contentView.backgroundColor;
    return cell;
}

It seems that my dequeued reuseable cells get their background forced to white on iPad. I was able to determine this using the view hierarchy debugger.

Once I did this I was able to use the table's background color and didn't have to set a background view, although that works as well.

2
Ray W On

Building off of Ben Flynn's answer... cell.contentView background color is not necessarily equal to the cell.background color. In which case, I found that this worked in resolving the iPad white background issue in more situations:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
        cell.backgroundColor = cell.backgroundColor;
        return cell;
    }

While the statement looks ridiculous and crazy... it resolves the issue.

4
John Stephen On

Instead of setting the background color, trying using a background view instead, like this:

- (void)viewDidLoad {
    self.tableView.backgroundView = [UIView new];
    self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}

I've had problems where using the backgroundColor doesn't always produce an effect, but setting a background view instead works fine.

2
Sterling Christensen On

In my experience, some versions of iOS set UITableViewCell's backgroundColor before calling the delegate's tableView:willDisplayCell:forRowAtIndexPath:. Resetting back to your custom color in that method fixes it.

4
Kiko Lobo On

You can fix this by making an appearance API setting in your appDelegate file :

Swift:

UITableViewCell.appearance().backgroundColor = UIColor.clearColor()
0
Animesh Porwal On

This seems to be fixed with iOS 10 Beta 4 as mentioned in release notes under UIKit notes:

enter image description here

0
d.rozumeenko On

I'm using Storyboard with UITableViewControlrs, so the most simple decision was to subclass all controllers, and add this method to parent

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        cell.backgroundColor = [UIColor clearColor];
    }
}
2
nahung89 On

Swift 5

I have a table view with many different cells inside, each one has different color.

The answer from @Ben Flynn cell.backgroundColor = self.contentView.backgroundColor can not achieve that. The reason is self.contentView.backgroundColor is nil, so what you did is just clear the cell.backgroundColor = nil.

Basically it is the bug from Xcode (I think, yeah it sucks!), cell.backgroundColor still has color, but it can not display.

After debug for a while, based on @Ray W answer, here is my solution.

class YourCustomCell: UICollectionViewCell {
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = backgroundColor // Tricky to re-apply the background color
    }
}
0
Ram Y On

This solve this issue for me

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    tableView.backgroundColor = UIColor.clear
}
0
Mark80 On

This can be achieved in a Storyboard as follows:

  • Show the document outline for your storyboard
  • Within your table, pick a TableViewCell
  • go to the Content View within that Cell
  • Set the background colour of the Content view.

Result: iPad and iPhone simulator views look the same

0
Diego Rebosio On

I just had this issue and fixed it by changing the backgroundColor of the View (as opposed to the one of the tableView). Seems on iPad, that's the one that is used first and in my case it was set to white.

0
Daniel Saidi On

I have a transparent table view with semi-transparent table view cells. I set the table view background color to clear when I create the table. This works for all iOS/device combinations except iPad + iOS 8, where the background color remains white.

For me, setting the cell's background color to semi-transparent and the content view background color to clear works, since I do it on each tableView(_ tableView: UITableView, cellForRowAt indexPath). The problem for me was only that the table view background remained white.

I tried all combinations that I did find regarding this issue, but the only thing I actually needed to to was to set the table views background to transparent on each tableView(_ tableView: UITableView, cellForRowAt indexPath). Not super-intuitive, but at least it works. :P

0
A.J. Hernandez On

SWIFT

This was a happening to me, too. My table view in my SWRevealViewController appeared white on my iPad when it looked clear (which is how I wanted it with a background image) on my iPhone. I tried all of the above but this is what ended up working for me in my viewDidLoad().

tableView.backgroundView = UIImageView(image: UIImage(named: "gray"))
tableView.backgroundView?.backgroundColor = .clearColor()
UITableViewCell.appearance().backgroundColor = .clearColor()
0
Abhishek Mitra On

SWIFT 3.XX

Put this

UITableViewCell.appearance().backgroundColor = UIColor.clear

In AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
0
Steve On

SWIFT I had this problem too. Works fine on .phone but tableViewCells go white on .pad. Thought I'd show how I fixed it using swift.

Connect The tableViewCell to the viewController.swift as an @IBOutlet like so:

    @IBOutlet weak var tvc1: UITableViewCell!
    @IBOutlet weak var tvc2: UITableViewCell!
    @IBOutlet weak var tvc3: UITableViewCell!

Then in viewDidLoad put the following:

tvc1.backgroundColor = tvc1.backgroundColor
tvc2.backgroundColor = tvc2.backgroundColor
tvc3.backgroundColor = tvc3.backgroundColor

Very strange, I don't know whats happening here but this solved it for me.

0
Xavi Moll On

Swift 3.1

I've worked around this bug by placing this in my UITableViewCell subclass:

When the cell is being loaded from the NIB file:

override func awakeFromNib() {
    super.awakeFromNib()
    self.backgroundColor = UIColor.clear
}

and when the cell is being reused by the system

override func prepareForReuse() {
    super.prepareForReuse()
    self.backgroundColor = UIColor.clear
}

iOS 10 is supposed to fix this issue in Interface Builder, as animeshporwal said.