UITableView backgroundColor always white on iPad

2019-01-16 13:54发布

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条回答
Animai°情兽
2楼-- · 2019-01-16 13:59

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

查看更多
Anthone
3楼-- · 2019-01-16 14:01

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.

查看更多
Deceive 欺骗
4楼-- · 2019-01-16 14:02

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.

查看更多
你好瞎i
5楼-- · 2019-01-16 14:03

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.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-16 14:03

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

enter image description here

查看更多
7楼-- · 2019-01-16 14:05

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

Swift:

UITableViewCell.appearance().backgroundColor = UIColor.clearColor()
查看更多
登录 后发表回答