【问题标题】:uitableview setbackgroundcolor not setting the color in UITableViewStyleGrouped tableuitableview setbackgroundcolor 未在 UITableViewStyleGrouped 表中设置颜色
【发布时间】:2023-03-29 18:55:01
【问题描述】:

在 iOS 6 中

[uitableview setBackgroundColor:]表格样式为UITableViewStyleGrouped时不设置颜色

而是看到默认的条纹背景。

如果样式是UITableViewStyleGrouped,我们应该如何设置表格的背景

【问题讨论】:

    标签: ios6


    【解决方案1】:
    [tableViewInstance setBackgroundView: nil];
    

    【讨论】:

    • 是的,这为我解决了问题。我使用的语法是:[self.tableView setBackgroundView: nil];
    • 请注意,此解决方案可能会给您在 iOS 5 中带来问题(如果您删除了 backgroundColor 语句),因为 iOS 将选择默认的 tableView 背景。
    【解决方案2】:

    设置

    [tableView setBackgroundView: nil];
    

    在 iOS 5 中给我带来了问题,所以我使用的是:

    UIView* bview = [[UIView alloc] init];
    bview.backgroundColor = [UIColor yellowColor];
    [tableView setBackgroundView:bview];
    

    iOS 5 和 6 兼容!

    【讨论】:

    • 谢谢,我正在找这个!
    • 如果你需要透明背景,你可以在我使用黄色的地方使用清晰的颜色。
    【解决方案3】:
    self.view.backgroundColor = TTSTYLEVAR(mainPageBackground);
    self.tableView.separatorColor   = TTSTYLEVAR(mainPageBackground);
    self.tableView.backgroundView = nil;
    

    为我修好了。不过,您必须小心这可能产生的其他影响。

    【讨论】:

    • 这将如何影响 iOS 5 和 iOS 4?
    • 我没有看到任何内容,但如果您不确定,您可以随时使用以下命令将其关闭:if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
    • 只要您使用 Xcode 4.5 构建应用程序,这种情况总会发生。我有一个 iOS 5 项目,这个问题也存在。使用“self.tableView.backgroundView = nil;”解决问题。
    【解决方案4】:

    另一个简单的解决方案是在 IB 中更改 UITableView 的背景颜色。例如,我改为“白色”,它又可以工作了。

    不知何故,将背景颜色保留为“默认”使得 iOS 6 不会打扰代码中的任何其他颜色设置。

    【讨论】:

    • 工作就像一个魅力......简单且向后兼容,感谢塞巴斯蒂安!
    【解决方案5】:

    我会谨慎将 backgroundView 设置为 nil。以我的经验,这会导致用户在最新的 XCode 4.5 下滚动时显着降低渲染性能。这是我对这个问题的解决方案,但不会影响滚动性能:

    - (void) viewWillLayoutSubviews
    {
        CGRect rect = [[UIScreen mainScreen] bounds];
        CGRect frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    
        UIView *backgroundView = [[[UIView alloc] initWithFrame:frame] autorelease];
        backgroundView.backgroundColor = [UIColor clearColor];
        self.tableView.backgroundView = backgroundView;
    }
    

    【讨论】:

    • 这对我有用。似乎是避免使用零背景视图产生任何意外后果的好方法。
    • 我不会在 viewWillLayoutSubviews 中设置你的背景视图,而是在 viewDidLoad 中只设置一次并使用自动调整大小的掩码。您实现调整 backgroundView 大小的方式可能会给您带来性能问题。
    【解决方案6】:

    在 ios 6 中,分组表视图的方法,即 backgroundColor 已被弃用,因此改为使用

    [tableview setBackgroundView : nil];
    

    【讨论】:

      【解决方案7】:

      我知道这已被标记为已回答,但以下内容也可以:

      [[UITableView appearance] setBackgroundColor:[UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0]];
      
      [[UITableView appearance] setBackgroundView:nil];
      

      这样您就不必在应用程序中为每个 tableView 设置。感谢作者的原始提示!

      【讨论】:

        【解决方案8】:

        将背景设置为 nil 可以完成这项工作,是的,它不会与以前的版本有任何问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-04
          • 2011-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多