ChatGPT解决这个技术问题 Extra ChatGPT

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

当我为我的 UITableView 设置 backgroundColor 时,它在 iPhone(设备和模拟器)上运行良好,但在 iPad 模拟器上却不行。相反,对于我设置的任何颜色(包括 groupTableViewBackgroundColor),我都会得到浅灰色背景。

重现步骤:

创建一个新的基于导航的项目。打开 RootViewController.xib 并将表格视图样式设置为“分组”。将此响应程序添加到 RootViewController: - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; } 选择 Simulator SDK 3.2,构建并运行。您将获得黑色背景(设备和模拟器)。在项目树中选择您的目标。单击项目:升级 iPad 的当前目标。构建并运行。你会得到一个浅灰色的背景。将表格视图样式恢复为普通,您将获得黑色背景。

谢谢你的帮助!

啊,现在 iPhone 和 iPad 在 iOS6 中再次对齐 :-)
为使用很少使用的术语“复制步骤”而喝彩! :-p

W
Wayne Hartman

尝试其中之一。

[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[[UIView alloc] init] autorelease]];

我不知道是否有副作用,但它有效!谢谢! self.tableView.backgroundView = nil;
非常非常重要:这仅适用于 SDK 3.2。为了向后兼容 3.1.3。之前你必须检查表视图是否响应 backgroundView 属性: if ([self.tableView respondsToSelector:@selector(backgroundView)]) self.tableView.backgroundView = nil;否则你的应用程序会崩溃并突然退出,你可以相信我!
是的,我尝试了上述解决方案,它对我有用。谁能告诉我这个问题到底是什么。为什么当我们运行应用程序时,它是灰色的?
请有人告诉我们为什么需要这样做来解决这个问题?
因为很明显新的 ios 可以让你放置一个背景视图。为什么它设置为一些灰色视图只是 Apple 引入的一个问题。非常奇怪,非常愚蠢。但话说回来,我每天在为 iOS 编码时都会遇到一些奇怪而愚蠢的事情
A
Alexander

非常感谢这个解决方案。我在 UIViewController 中使用 IBOutlet 将其应用于 UITableView 属性,并且效果很好:

[panelTable setBackgroundView:nil];
[panelTable setBackgroundView:[[[UIView alloc] init] autorelease]];
[panelTable setBackgroundColor:UIColor.clearColor]; // Make the table view transparent

中间线没有任何必要 [panelTable setBackgroundView:[[[UIView alloc] init] autorelease]];
冰火:其实是有的。没有它,您会在表格单元格边框中得到奇怪的绘图伪影。
P
Philipp Frischmuth

在 iPad 上,backgroundView 属性似乎用于为分组表创建灰色背景颜色。因此,要在 iPad 上更改分组表格的背景颜色,应该 nilbackgroundView 属性,然后在所需的表格视图上设置 backgroundColor

- (void)viewDidLoad
{
    [super viewDidLoad];

    // If you need to support iOS < 3.2, check for the availability of the
    // backgroundView property.
    if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) {
        self.tableView.backgroundView = nil;
    }
    self.tableView.backgroundColor = [UIColor whiteColor];
}

是的,应该添加此检查以支持早期的 iOS。
W
WiseGuy

我认为值得注意的是,从 Xcode 6.1 和 iOS 8.1 开始,特别是对于 iPad(如果您还想设置单元格背景),您似乎必须设置表格背景和单元格背景。

例如,在 iPhone 故事板上,您可以将单元格设置为清除颜色,然后以编程方式为带有背景图像的透明表格设置表格的背景图像。但是,如果您要在 iPad 上查看相同的配置,则单元格将不清楚。需要将单元格设置为以编程方式为 iPad 清除。


对于使用 Xcode 6.4 构建并在 iOS 9.2 上运行的应用程序,我看到了这个问题。我在 Interface Builder 中将(静态)表格视图单元格的背景颜色设置为清除,它们在 iPhone 上看起来很清晰,但在 iPad 上它们是白色的,直到我在视图控制器的 -viewDidLoad: 方法中将单元格的背景颜色设置为清除。
c
cube108

尝试为表格设置 backgroundColor 和 View,没有运气(Xcode 5 iOS7.1 iPad 模拟器),在 iPhone 上看起来不错,但在 iPad 上是浅灰色背景色......

使用单元格本身的 backgroundColor 为我在 iOS 7.1 4/2014 上解决了这个问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"ViewSomeTriggerCell";

        // get a cell or a recycled cell
        sictVCViewSomeTriggerCell *cellTrigger = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // put a picture and a word in the cell (positions set in .xib)
        NSString * tname = [self.fetchedTriggersArray objectAtIndex:indexPath.row];
        cellTrigger.imageTrigger.image = [UIImage imageNamed:@"icon_appicon.png"];
        cellTrigger.labelName.text = tname;

        // set background color, defeats iPad wierdness
        // http://stackoverflow.com/questions/2688007/uitableview-backgroundcolor-always-gray-on-ipad

        // clearColor is what I wanted, and it worked, also tested with purpleColor
        cellTrigger.backgroundColor =[UIColor clearColor];
        return cellTrigger;

}


g
gmogames

如果您的应用程序同时针对 iPhone 和 iPad,您可以这样做:

[myTableView setBackgroundColor:[UIColor clearColor]]; // this is for iPhone

if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone) {
    [myTableView setBackgroundView:nil];
    [myTableView setBackgroundView:[[UIView alloc] init]];
} // this is for iPad only

请注意,TableView 分配器没有 ARC 的自动释放功能。


i
irmakoz

如果您将 UIViewController 添加到另一个 UIViewController 中,除了 UITableView 之外,您还需要将视图的背景设置为 clearColor ,否则您将获得白色背景而不是浅灰色。

if ([myViewController.myTableView respondsToSelector:@selector(setBackgroundView:)]) {
    [myViewController.myTableView setBackgroundView:nil];
}
myViewController.myTableView.backgroundColor = [UIColor clearColor];
myViewController.view.backgroundColor = [UIColor clearColor];

Q
QED

模拟 iPad 2

运行 iOS 7.1 到 8.3

从 XCode 6.3 构建

以上都不适用于我使用单个 XIB 指定的 UIViewController->UITableView。起作用的是将整个设置移动到情节提要中,并使用 IB 检查器设置背景颜色。


e
elegance66

我也遇到了这种问题。无论我设置什么,UITableView 背景颜色都将始终为 groupTableViewBackgroundColor

症状就像这个问题 - UITableView is resetting its background color before view appears

我在 init 函数中设置背景颜色后,它是正确的。在viewDidLoadviewWillAppear阶段,它是正确的。但是,在 viewDidAppear 中,背景颜色被重置为其默认颜色。

接受的答案现在不起作用。

[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[UIView alloc] init]];

这是我的解决方案。 只需在 viewDidLoad 函数中设置 tableView 的背景颜色,而不是 initviewWillAppear

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