ChatGPT解决这个技术问题 Extra ChatGPT

UITableView backgroundColor always white on iPad

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?

Are you using Xcode 6 with size classes? You can set different background colors depending on the size class you're using.
@remus Nope, I work on Xcode 6 but I disabled size classes.
Set a break point when you're making your cells, and go to Debug -> View Debugging -> Capture View Hierarchy. This will let you examine every subview of your view stack so you can see exactly which view is causing the issue. Note, I think this only works in the simulator
set the contectview and cell to clear color
@Chris I tried on real device one week ago :)

B
Ben Flynn

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.


h/t to @SterlingChristensen whose answer this builds off of.
Bizarrely, this works for me. Filed a radar: openradar.appspot.com/radar?id=5187188266369024
This works, though, it should be noted that may cell.contentView.backgroundColor returns nil even if IB contains a value, so it may not work when the background colors of the UITableView vary from its cells.
I don't know how apple developers think!
Thanks, works great for me. Im my case I needed the reverse cell.contentView.backgroundColor = cell.backgroundColor. After I printed cell.contentView.backgroundColor and noticed it was nil before, I removed your suggestion and changed color of the content view in the storyboard which also did work :)
K
Kiko Lobo

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

Swift:

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

This solution saved my life!. Thanks
Worked like a charm! In Objective C: [UITableViewCell appearance].backgroundColor = [UIColor clearColor];
the only solution that worked for me! thanks a bunch!!
Nice, simple solution!
J
John Stephen

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.


Yes it worked, thank you. But it's really strange! TableView's clearColor works on iPhone but why is not working on iPad. What might be the difference?
The pad has to support some UI models the phone doesn't, like split view controllers and popovers, maybe that has something to do with it. I wonder if it happens on the iPhone 6+ since it has to support many of those same UI features.
It doesn't happen on the 6+
Does it remain white when you have enough table cells to fill the screen, or only on short tables? Seems somehow related to number of cells in a table.
R
Ray W

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.


Hi, I'm trying to get this working for Monotouch. I'm working with Xamarin.iOS , using a c# implementation. How can I implement your solution?
This trick didn't fix this problem on the iPad for me, sorry. cell.backgroundColor = [UIColor clearColor]; did though.
C
Community

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

Works for me, if I put it into a category like "@implementation UITableViewCell (fixiPadColors)".
should not do that way. I prefer @Ray W solution in your case. Check here for the reason stackoverflow.com/questions/9424004/…
R
Ram Y

This solve this issue for me

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

S
Sterling Christensen

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.


Yes this is the solution, even it's strange! I found it when I tried on iPad simulator iOS 8.2, but it was correct with all other iPhone versions. Just put [cell setBackgroundColor:[UIColor clearColor]]; to the above mentioned place.
Yah this answer also works as I already tried this, I had same issue in iOS 9 iPad tableveiw cell background colour changing to white while working fine in all iPhone
X
Xavi Moll

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.


A
Abhishek Mitra

SWIFT 3.XX

Put this

UITableViewCell.appearance().backgroundColor = UIColor.clear

In AppDelegate

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

T
Tomasz Jakub Rup

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

A
A.J. Hernandez

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

M
Mark80

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


D
Diego Rebosio

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.


S
Steve

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.


A
Animesh Porwal

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

https://i.stack.imgur.com/gBaTJ.png


D
Daniel Saidi

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