【问题标题】:UITableView backgroundColor always gray on iPadUITableView 背景颜色在 iPad 上总是灰色
【发布时间】:2011-02-10 21:27:24
【问题描述】:

当我为我的UITableView 设置backgroundColor 时,它在 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. 将表格视图样式恢复为纯色,您将获得黑色背景。

感谢您的帮助!

【问题讨论】:

  • 啊,现在 iPhone 和 iPad 在 iOS6 中再次对齐了 :-)
  • 非常感谢使用很少使用的术语“复制步骤”! :-p

标签: ipad uitableview iphone-sdk-3.2 background-color


【解决方案1】:

尝试其中一种。

[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 编码时都会遇到一些奇怪而愚蠢的事情
【解决方案2】:

非常感谢这个解决方案。 我在 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]];
  • 冰冻之火:确实有。没有它,您会在表格单元格边框中得到奇怪的绘图伪影。
【解决方案3】:

在 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];
}

【讨论】:

  • 是的,应该添加此检查以支持早期的 iO​​S。
【解决方案4】:

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

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

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

      }

      【讨论】:

        【解决方案7】:

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

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

        【讨论】:

        • 我在使用 Xcode 6.4 构建并在 iOS 9.2 上运行的应用程序中看到了这个问题。我在 Interface Builder 中将(静态)表格视图单元格的背景颜色设置为清除,它们在 iPhone 上看起来很清晰,但在 iPad 上它们是白色的,直到我在视图控制器的 -viewDidLoad: 方法中将单元格的背景颜色设置为清除。
        【解决方案8】:
        • 模拟 iPad 2
        • 运行 iOS 7.1 到 8.3
        • 从 XCode 6.3 构建

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

        【讨论】:

          【解决方案9】:

          我也遇到过这种问题。无论我设置什么,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];
          }
          

          【讨论】:

            猜你喜欢
            • 2015-02-17
            • 1970-01-01
            • 1970-01-01
            • 2011-10-07
            • 2020-04-11
            • 1970-01-01
            • 1970-01-01
            • 2012-06-06
            • 1970-01-01
            相关资源
            最近更新 更多