ChatGPT解决这个技术问题 Extra ChatGPT

UITableView 背景颜色在 iPad 上总是白色

我正在做一个项目。我有很多 UITableView 设置为清晰的颜色。他们视图的背景颜色设置为我的自定义颜色,并且在 iPhone 上一切正常。

iPad 上出现了问题!我几乎尝试了所有方法,但我的 UITableView 是白色的。

我检查了其他主题,例如:UITableView backgroundColor always gray on iPad,但没有任何效果。还有,我的问题不是灰色,而是雪白的!

可能是什么原因?

你在使用 Xcode 6 和大小类吗?您可以根据使用的尺寸等级设置不同的背景颜色。
@remus 不,我在 Xcode 6 上工作,但我禁用了大小类。
在制作单元格时设置一个断点,然后转到 Debug -> View Debugging -> Capture View Hierarchy。这将让您检查视图堆栈的每个子视图,以便您可以准确地看到导致问题的视图。注意,我认为这只适用于模拟器
将 contectview 和单元格设置为清除颜色
@Chris 我一周前在真实设备上试过:)

B
Ben Flynn

好消息:根据发行说明,对于 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 上将背景强制变为白色。我能够使用视图层次结构调试器来确定这一点。

一旦我这样做了,我就可以使用表格的背景颜色并且不必设置背景视图,尽管它也可以。


h/t 给@SterlingChristensen,他的回答以此为基础。
奇怪的是,这对我有用。归档雷达:openradar.appspot.com/radar?id=5187188266369024
但是,应该注意的是,即使 IB 包含一个值,cell.contentView.backgroundColor 也可能返回 nil,因此当 UITableView 的背景颜色与其单元格不同时,它可能不起作用。
不知道苹果开发者是怎么想的!
谢谢,对我很有用。我的情况是我需要相反的cell.contentView.backgroundColor = cell.backgroundColor。在我打印 cell.contentView.backgroundColor 并注意到它之前是 nil 之后,我删除了您的建议并更改了情节提要中内容视图的颜色,这也确实有效:)
K
Kiko Lobo

您可以通过在 appDelegate 文件中进行外观 API 设置来解决此问题:

迅速:

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

这个解决方案救了我的命!谢谢
像魅力一样工作!在目标 C 中:[UITableViewCell appearance].backgroundColor = [UIColor clearColor];
唯一对我有用的解决方案!非常感谢!
不错,简单的解决方案!
J
John Stephen

而不是设置背景颜色,而是尝试使用背景视图,如下所示:

- (void)viewDidLoad {
    self.tableView.backgroundView = [UIView new];
    self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}

我遇到了使用 backgroundColor 并不总是产生效果的问题,但是设置背景视图却可以正常工作。


是的,它有效,谢谢。但这真的很奇怪! TableView 的 clearColor 在 iPhone 上工作,但为什么在 iPad 上不工作。可能有什么区别?
pad 必须支持一些手机不支持的 UI 模型,比如拆分视图控制器和弹出框,这可能与它有关。我想知道它是否发生在 iPhone 6+ 上,因为它必须支持许多相同的 UI 功能。
它不会发生在 6+
当您有足够的表格单元格填满屏幕时,它会保持白色,还是仅在短表格上?似乎与表格中的单元格数量有关。
R
Ray W

以 Ben Flynn 的回答为基础... cell.contentView 背景颜色不一定等于 cell.background 颜色。在这种情况下,我发现这可以在更多情况下解决 iPad 白色背景问题:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
        cell.backgroundColor = cell.backgroundColor;
        return cell;
    }

虽然该声明看起来荒谬和疯狂……但它解决了问题。


嗨,我正试图让这个为 Monotouch 工作。我正在使用 Xamarin.iOS ,使用 ac# 实现。我该如何实施您的解决方案?
这个技巧并没有为我解决 iPad 上的这个问题,抱歉。 cell.backgroundColor = [UIColor clearColor];虽然做了。
C
Community

斯威夫特 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
    }
}

如果我将其放入“@implementation UITableViewCell (fixiPadColors)”之类的类别中,则对我有用。
不应该那样做。在您的情况下,我更喜欢@Ray W 解决方案。检查这里的原因stackoverflow.com/questions/9424004/…
R
Ram Y

这为我解决了这个问题

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

S
Sterling Christensen

根据我的经验,某些版本的 iOS 在调用委托的 tableView:willDisplayCell:forRowAtIndexPath: 之前设置了 UITableViewCell 的 backgroundColor。以该方法重置回您的自定义颜色可以修复它。


是的,这就是解决方案,即使它很奇怪!我在 iPad 模拟器 iOS 8.2 上尝试时发现了它,但在所有其他 iPhone 版本上都是正确的。只要把 [cell setBackgroundColor:[UIColor clearColor]];到上面提到的地方。
是的,这个答案也适用,因为我已经尝试过了,我在 iOS 9 iPad tableveiw 单元格背景颜色变为白色时遇到了同样的问题,而在所有 iPhone 上都可以正常工作
X
Xavi Moll

斯威夫特 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 中解决这个问题。


A
Abhishek Mitra

斯威夫特 3.XX

把这个

UITableViewCell.appearance().backgroundColor = UIColor.clear

AppDelegate

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

T
Tomasz Jakub Rup

我将 StoryboardUITableViewControlrs 一起使用,所以最简单的决定是对所有控制器进行子类化,并将此方法添加到父级

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        cell.backgroundColor = [UIColor clearColor];
    }
}

A
A.J. Hernandez

迅速

这也发生在我身上。我的 SWRevealViewController 中的表格视图在我的 iPhone 上看起来很清晰(这就是我想要的背景图像)在我的 iPad 上显示为白色。我尝试了以上所有方法,但这最终在我的 viewDidLoad() 中为我工作。

tableView.backgroundView = UIImageView(image: UIImage(named: "gray"))
tableView.backgroundView?.backgroundColor = .clearColor()
UITableViewCell.appearance().backgroundColor = .clearColor()

M
Mark80

这可以在 Storyboard 中实现,如下所示:

显示故事板的文档大纲

在您的表格中,选择一个 TableViewCell

转到该单元格中的内容视图

设置内容视图的背景颜色。

结果:iPad 和 iPhone 模拟器视图看起来相同


D
Diego Rebosio

我刚刚遇到了这个问题,并通过更改视图的背景颜色(而不是 tableView 的一个)来修复它。似乎在 iPad 上,这是第一个使用的,在我的情况下它被设置为白色。


S
Steve

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

很奇怪,我不知道这里发生了什么,但这为我解决了。


A
Animesh Porwal

UIKit 注释下的发行说明中所述,这似乎已在 iOS 10 Beta 4 中得到修复:

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


D
Daniel Saidi

我有一个带有半透明表格视图单元格的透明表格视图。创建表格时,我将表格视图背景颜色设置为清除。这适用于除 iPad + iOS 8 之外的所有 iOS/设备组合,其中背景颜色保持白色。

对我来说,将单元格的背景颜色设置为半透明并将内容视图的背景颜色设置为清除是可行的,因为我在每个 tableView(_ tableView: UITableView, cellForRowAt indexPath) 上都这样做。对我来说问题只是表格视图背景仍然是白色的。

我尝试了我找到的关于这个问题的所有组合,但我真正需要做的唯一事情是在每个 tableView(_ tableView: UITableView, cellForRowAt indexPath) 上将 table views 背景设置为透明。不是超级直观,但至少它有效。 :P