我正在做一个项目。我有很多 UITableView
设置为清晰的颜色。他们视图的背景颜色设置为我的自定义颜色,并且在 iPhone 上一切正常。
iPad 上出现了问题!我几乎尝试了所有方法,但我的 UITableView
是白色的。
我检查了其他主题,例如:UITableView backgroundColor always gray on iPad,但没有任何效果。还有,我的问题不是灰色,而是雪白的!
可能是什么原因?
好消息:根据发行说明,对于 iOS 10:
在 iPad 上运行时,Storyboard 中为 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()
[UITableViewCell appearance].backgroundColor = [UIColor clearColor];
而不是设置背景颜色,而是尝试使用背景视图,如下所示:
- (void)viewDidLoad {
self.tableView.backgroundView = [UIView new];
self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}
我遇到了使用 backgroundColor 并不总是产生效果的问题,但是设置背景视图却可以正常工作。
以 Ben Flynn 的回答为基础... cell.contentView 背景颜色不一定等于 cell.background 颜色。在这种情况下,我发现这可以在更多情况下解决 iPad 白色背景问题:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.backgroundColor = cell.backgroundColor;
return cell;
}
虽然该声明看起来荒谬和疯狂……但它解决了问题。
斯威夫特 5
我有一个表格视图,里面有许多不同的单元格,每个单元格都有不同的颜色。
@Ben Flynn cell.backgroundColor = self.contentView.backgroundColor
的回答无法做到这一点。原因是 self.contentView.backgroundColor
为零,所以您所做的只是清除 cell.backgroundColor = nil
。
基本上它是来自 Xcode 的错误(我想,是的,它很烂!),cell.backgroundColor
仍然有颜色,但它无法显示。
经过一段时间的调试,基于@Ray W的回答,这是我的解决方案。
class YourCustomCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = backgroundColor // Tricky to re-apply the background color
}
}
这为我解决了这个问题
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.backgroundColor = UIColor.clear
}
根据我的经验,某些版本的 iOS 在调用委托的 tableView:willDisplayCell:forRowAtIndexPath:
之前设置了 UITableViewCell 的 backgroundColor。以该方法重置回您的自定义颜色可以修复它。
斯威夫特 3.1
我通过将它放在我的 UITableViewCell
子类中解决了这个错误:
当从 NIB 文件加载单元格时:
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor.clear
}
以及当单元被系统重用时
override func prepareForReuse() {
super.prepareForReuse()
self.backgroundColor = UIColor.clear
}
正如animeshporwal 所说,iOS 10 应该在 Interface Builder 中解决这个问题。
斯威夫特 3.XX
把这个
UITableViewCell.appearance().backgroundColor = UIColor.clear
在 AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
我将 Storyboard
与 UITableViewControlrs
一起使用,所以最简单的决定是对所有控制器进行子类化,并将此方法添加到父级
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
cell.backgroundColor = [UIColor clearColor];
}
}
迅速
这也发生在我身上。我的 SWRevealViewController 中的表格视图在我的 iPhone 上看起来很清晰(这就是我想要的背景图像)在我的 iPad 上显示为白色。我尝试了以上所有方法,但这最终在我的 viewDidLoad() 中为我工作。
tableView.backgroundView = UIImageView(image: UIImage(named: "gray"))
tableView.backgroundView?.backgroundColor = .clearColor()
UITableViewCell.appearance().backgroundColor = .clearColor()
这可以在 Storyboard 中实现,如下所示:
显示故事板的文档大纲
在您的表格中,选择一个 TableViewCell
转到该单元格中的内容视图
设置内容视图的背景颜色。
结果:iPad 和 iPhone 模拟器视图看起来相同
我刚刚遇到了这个问题,并通过更改视图的背景颜色(而不是 tableView 的一个)来修复它。似乎在 iPad 上,这是第一个使用的,在我的情况下它被设置为白色。
SWIFT 我也有这个问题。在 .phone 上工作正常,但 tableViewCells 在 .pad 上变白。以为我会展示如何使用 swift 修复它。
将 tableViewCell 作为 @IBOutlet 连接到 viewController.swift,如下所示:
@IBOutlet weak var tvc1: UITableViewCell!
@IBOutlet weak var tvc2: UITableViewCell!
@IBOutlet weak var tvc3: UITableViewCell!
然后在 viewDidLoad 中输入以下内容:
tvc1.backgroundColor = tvc1.backgroundColor
tvc2.backgroundColor = tvc2.backgroundColor
tvc3.backgroundColor = tvc3.backgroundColor
很奇怪,我不知道这里发生了什么,但这为我解决了。
如 UIKit
注释下的发行说明中所述,这似乎已在 iOS 10 Beta 4 中得到修复:
https://i.stack.imgur.com/gBaTJ.png
我有一个带有半透明表格视图单元格的透明表格视图。创建表格时,我将表格视图背景颜色设置为清除。这适用于除 iPad + iOS 8 之外的所有 iOS/设备组合,其中背景颜色保持白色。
对我来说,将单元格的背景颜色设置为半透明并将内容视图的背景颜色设置为清除是可行的,因为我在每个 tableView(_ tableView: UITableView, cellForRowAt indexPath)
上都这样做。对我来说问题只是表格视图背景仍然是白色的。
我尝试了我找到的关于这个问题的所有组合,但我真正需要做的唯一事情是在每个 tableView(_ tableView: UITableView, cellForRowAt indexPath)
上将 table views 背景设置为透明。不是超级直观,但至少它有效。 :P
cell.contentView.backgroundColor = cell.backgroundColor
。在我打印cell.contentView.backgroundColor
并注意到它之前是nil
之后,我删除了您的建议并更改了情节提要中内容视图的颜色,这也确实有效:)