128

当我backgroundColor为我设置UITableView它时,它可以在 iPhone(设备和模拟器)上正常工作,但不能在 iPad 模拟器上工作。相反,我为我设置的任何颜色(包括groupTableViewBackgroundColor.

重现步骤:

  1. 创建一个新的基于导航的项目。
  2. 打开 RootViewController.xib 并将表格视图样式设置为“分组”。
  3. 将此响应程序添加到 RootViewController:

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    }
  4. 选择 Simulator SDK 3.2,构建并运行。
  5. 您将获得黑色背景(设备和模拟器)。
  6. 在项目树中选择您的目标。
  7. 单击项目:升级 iPad 的当前目标。
  8. 构建并运行。
  9. 你会得到一个浅灰色的背景。
  10. 将表格视图样式恢复为纯色,您将获得黑色背景。

谢谢你的帮助!

4

9 回答 9

245

尝试其中之一。

[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[[UIView alloc] init] autorelease]];
于 2010-04-22T03:52:09.233 回答
30

非常感谢这个解决方案。我在 UIViewController 中使用 IBOutlet 在 UITableView 属性上应用了它,它的效果很好:

[panelTable setBackgroundView:nil];
[panelTable setBackgroundView:[[[UIView alloc] init] autorelease]];
[panelTable setBackgroundColor:UIColor.clearColor]; // Make the table view transparent
于 2010-06-15T15:13:17.450 回答
6

在 iPad 上,该backgroundView属性似乎用于为分组表创建灰色背景色。因此,要在 iPad 上更改分组表的背景颜色,应该nil退出该backgroundView属性,然后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];
}
于 2012-04-20T10:37:56.563 回答
2

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

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

于 2014-12-08T19:04:18.970 回答
1

尝试为表格设置 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;

}

于 2014-04-02T01:59:27.277 回答
0

如果您的应用程序同时针对 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 的自动释放功能。

于 2012-06-14T14:33:58.667 回答
0

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

if ([myViewController.myTableView respondsToSelector:@selector(setBackgroundView:)]) {
    [myViewController.myTableView setBackgroundView:nil];
}
myViewController.myTableView.backgroundColor = [UIColor clearColor];
myViewController.view.backgroundColor = [UIColor clearColor];
于 2012-07-12T07:29:15.670 回答
0
  • 模拟 iPad 2
  • 运行 iOS 7.1 到 8.3
  • 从 XCode 6.3 构建

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

于 2015-04-16T19:06:31.530 回答
0

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

症状就像这个问题 - UITableView 在视图出现之前重置其背景颜色

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

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

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

Here is my solution. Just set the tableView's background color in viewDidLoad function rather than init or viewWillAppear.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = [UIColor clearColor];
}
于 2018-11-26T07:43:43.353 回答