我正在做一个项目.我有很多UITableView
s设置为清晰的颜色.他们的观点背景颜色设置为我的自定义颜色,iPhone上的一切都很好.
这个问题出现在iPad上!我几乎尝试了所有东西,但我UITableView
的颜色是白色.
我检查了其他主题,例如:UITableView backgroundColor在iPad上总是灰色,但没有任何效果.另外,我的问题不是灰色,它像雪一样白!
可能是什么原因?
这为我解决了这个问题
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { tableView.backgroundColor = UIColor.clear }
好消息:根据发布说明,适用于iOS 10:
在iPad上运行时,现在可以遵守故事板中UITableViewCell的背景颜色设置.
对于版本<10:
我在iOS 8(8.3)中看到了这一点.即使在IB中我的细胞是"清晰的颜色",并且它们的内容视图是"清晰的颜色",它们将呈现为白色.一个不完美但合理的解决方案,因为它仍然需要来自IB的值:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... cell.backgroundColor = cell.contentView.backgroundColor; return cell; }
似乎我的可重复使用的可重复使用的单元格在iPad上将其背景强制为白色.我能够使用视图层次结构调试器来确定这一点.
一旦我这样做,我就可以使用表格的背景颜色,而不必设置背景视图,尽管这样也可以.
您可以通过在appDelegate文件中进行外观API设置来解决此问题:
迅速:
UITableViewCell.appearance().backgroundColor = UIColor.clearColor()
而不是设置背景颜色,尝试使用背景视图,如下所示:
- (void)viewDidLoad { self.tableView.backgroundView = [UIView new]; self.tableView.backgroundView.backgroundColor = [UIColor clearColor]; }
我有一些问题,使用backgroundColor并不总是产生效果,但设置背景视图反而很好.
我有一个桌面视图,里面有许多不同的单元格,每个单元格都有不同的颜色.
@Ben Flynn的答案cell.backgroundColor = self.contentView.backgroundColor
无法实现.原因是self.contentView.backgroundColor
零,所以你所做的只是清楚了cell.backgroundColor = nil
.
基本上它是来自Xcode的错误(我想,是的,它糟透了!),cell.backgroundColor
仍然有颜色,但它无法显示.
经过一段时间的调试,基于@Ray W答案,这是我的解决方案.
@impelement YouCustomCellClass - (void)awakeFromNib { [super awakeFromNib]; self.backgroundColor = self.backgroundColor; // What the heck?? But it is. } @end
建立Ben Flynn的答案... cell.contentView背景颜色不一定等于cell.background颜色.在这种情况下,我发现这在解决更多情况下的iPad白色背景问题方面有效:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... cell.backgroundColor = cell.backgroundColor; return cell; }
虽然声明看起来很荒谬和疯狂......它解决了这个问题.