iPad 上的 UITableView 背景颜色始终为灰色

发布于 2024-08-29 14:44:43 字数 638 浏览 10 评论 0原文

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

重现步骤:

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

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

感谢您的帮助!

When I set the backgroundColor for my UITableView it works fine on iPhone (device and simulator) but NOT on the iPad simulator. Instead I get a light gray background for any color I set including groupTableViewBackgroundColor.

Steps to reproduce:

  1. Create a new navigation-based project.
  2. Open RootViewController.xib and set the table view style to "Grouped".
  3. Add this responder to the RootViewController:
    - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    }
  4. Select Simulator SDK 3.2, build and run.
  5. You will get a black background (device and simulator).
  6. Select your target in the project tree.
  7. Click on Project : Upgrade Current Target for iPad.
  8. Build and run.
  9. You will get a light gray background.
  10. Revert the table view style to Plain and you will get a black background.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

夢归不見 2024-09-05 14:44:44

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

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

If you are adding a UIViewController to another UIViewController you also need to set your view's background to clearColor in addition to UITableView or else you will get a white background instead of light grey.

if ([myViewController.myTableView respondsToSelector:@selector(setBackgroundView:)]) {
    [myViewController.myTableView setBackgroundView:nil];
}
myViewController.myTableView.backgroundColor = [UIColor clearColor];
myViewController.view.backgroundColor = [UIColor clearColor];
梦与时光遇 2024-09-05 14:44:44
  • 模拟 iPad 2
  • 运行 iOS 7.1 到 8.3
  • 从 XCode 6.3 构建

以上都不适用于使用单个 XIB 指定的 UIViewController->UITableView。有效的方法是将整个设置移动到故事板中,并使用 IB 检查器设置背景颜色。

  • Simulating iPad 2
  • Running iOS 7.1 through 8.3
  • Built from XCode 6.3

None of the above worked for my UIViewController->UITableView specified using a single XIB. What did work was moving the whole setup into a Storyboard, and setting the background color using the IB inspector.

枫以 2024-09-05 14:44:44

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

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

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

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

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

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

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

I also got this kind of problem. The UITableView background color will be always groupTableViewBackgroundColor no matter what I set.

The symptom is like this issue - UITableView is resetting its background color before view appears

After I set the background color in init function, it is correct. In viewDidLoad and viewWillAppear stage, it is correct. However, in viewDidAppear, the background color is reset to its default color.

The accepted answer does not work now.

[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];
}
木落 2024-09-05 14:44:43

尝试其中之一。

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

Try one of these.

[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[[UIView alloc] init] autorelease]];
思念满溢 2024-09-05 14:44:43

非常感谢这个解决方案。
我将其应用到 UIViewController 中带有 IBOutlet 的 UITableView 属性上,效果很好,如下所示:

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

Thanks a lot for this solution.
I applied this on a UITableView property with IBOutlet in a UIViewController and it works well like:

[panelTable setBackgroundView:nil];
[panelTable setBackgroundView:[[[UIView alloc] init] autorelease]];
[panelTable setBackgroundColor:UIColor.clearColor]; // Make the table view transparent
可可 2024-09-05 14:44:43

在 iPad 上,backgroundView 属性似乎用于为分组表格创建灰色背景颜色。因此,要更改 iPad 上分组表格的背景颜色,应将 backgroundView 属性nil,然后在所需的表格视图上设置 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];
}

On iPad the backgroundView property seems to be used to create the gray background color for grouped tables. So for changing the background color for grouped tables on iPad one should nil out the backgroundView property and then set the backgroundColor on the desired table view.

- (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];
}
み格子的夏天 2024-09-05 14:44:43

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

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

I think it is worth noting that as of Xcode 6.1 and iOS 8.1, specifically for iPad (if you want to set cell background as well) it seems that you must set table background AND cell background.

For instance, on an iPhone storyboard you can set a cell to clear color, then set background image of table programmatically for a transparent table with background image. However if you were to view this same configuration on iPad the cells would not be clear. Cells will need to be set to clear programmatically for iPad.

一城柳絮吹成雪 2024-09-05 14:44:43

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

}

tried setting backgroundColor and View for the table, had no luck (Xcode 5 iOS7.1 iPad simulator), looked OK for iPhone, but light grey background color on iPad...

working with backgroundColor for the cell itself fixed this for me on 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;

}

断舍离 2024-09-05 14:44:43

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

If your application targets both iPhone and iPad you can do it like this:

[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

Notice that the TableView alloc doesn't have autorelease for ARC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文