UITableView backgroundColor always white on iPad

IosObjective CIpadUitableviewUibackgroundcolor

Ios Problem Overview


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: https://stackoverflow.com/questions/2688007/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?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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

Swift:

UITableViewCell.appearance().backgroundColor = UIColor.clearColor()

Solution 3 - Ios

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.

Solution 4 - Ios

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.

Solution 5 - Ios

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

Solution 6 - Ios

This solve this issue for me

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

Solution 7 - Ios

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.

Solution 8 - Ios

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.

Solution 9 - Ios

SWIFT 3.XX

Put this

UITableViewCell.appearance().backgroundColor = UIColor.clear

In AppDelegate

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

Solution 10 - Ios

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

Solution 11 - Ios

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()

Solution 12 - Ios

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

Solution 13 - Ios

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.

Solution 14 - Ios

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.

Solution 15 - Ios

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

enter image description here

Solution 16 - Ios

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

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
QuestionG&#246;ktuğ AralView Question on Stackoverflow
Solution 1 - IosBen FlynnView Answer on Stackoverflow
Solution 2 - IosKiko LoboView Answer on Stackoverflow
Solution 3 - IosJohn StephenView Answer on Stackoverflow
Solution 4 - IosRay WView Answer on Stackoverflow
Solution 5 - Iosnahung89View Answer on Stackoverflow
Solution 6 - IosRam YView Answer on Stackoverflow
Solution 7 - IosSterling ChristensenView Answer on Stackoverflow
Solution 8 - IosXavi MollView Answer on Stackoverflow
Solution 9 - IosAbhishek MitraView Answer on Stackoverflow
Solution 10 - Iosd.rozumeenkoView Answer on Stackoverflow
Solution 11 - IosA.J. HernandezView Answer on Stackoverflow
Solution 12 - IosMark80View Answer on Stackoverflow
Solution 13 - IosDiego RebosioView Answer on Stackoverflow
Solution 14 - IosSteveView Answer on Stackoverflow
Solution 15 - IosAnimesh PorwalView Answer on Stackoverflow
Solution 16 - IosDaniel SaidiView Answer on Stackoverflow