【问题标题】:is groupTableViewBackgroundColor deprecated on iOS 6?在 iOS 6 上不推荐使用 groupTableViewBackgroundColor 吗?
【发布时间】:2012-09-09 06:55:39
【问题描述】:

我刚刚使用 iOS 6.0 和 Xcode 4.5GM 测试我的应用程序,我设置了这样的视图:

[self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];

因此,视图与普通表格视图具有相同的模式。

这在 iOS 4 和 5 上运行良好,但在 iOS 6 中它只是给我一个白色背景。

这是否已弃用?如果可以,如何更换?

谢谢

【问题讨论】:

    标签: ios view background tableview ios6


    【解决方案1】:

    此方法将在 6.0 种子程序中被弃用 如果你想在你自己的视图中有一个看起来像表格视图背景的背景, 那么你应该创建一个空的表格视图并将其放在你的内容后面。

    【讨论】:

    • 好主意,空表视图!
    • 有点丑陋的建议哈哈,你应该让设计师制作背景图像会是一个更好的解决方案。由于已弃用的提示,无论如何都要投票。
    • 很奇怪,文档没有提到它已被弃用(但确实如此)
    【解决方案2】:

    在 iOS6 SKD 中,UIInterface.h 中的 cmets 建议如下:

    组样式表视图背景不能再由 一种简单的颜色。 如果您想在自己的视图中有背景 看起来像表格视图背景,那么您应该创建 一个空的表格视图并将其放在您的内容后面。

    此方法将在 6.0 种子程序中被弃用

    一个简单的解决方案是用等效的 RGB 值设置背景:

    [self.view setBackgroundColor:[UIColor colorWithRed:215.0/255.0 green:217.0/255.0 blue:223.0/255.0 alpha:1.0]];
    

    您可以在 xib 中将视图背景设置为白色或任何您喜欢的颜色以抑制警告。

    【讨论】:

      【解决方案3】:

      我写了一个UIColor 类别来替换groupTableViewBackgroundColor

      @interface UIColor (UITableViewBackground)
      + (UIColor *)groupTableViewBackgroundColor;    
      @end
      
      @implementation UIColor (UITableViewBackground)
      
      + (UIColor *)groupTableViewBackgroundColor
      {
          __strong static UIImage* tableViewBackgroundImage = nil;
          static dispatch_once_t onceToken;
          dispatch_once(&onceToken, ^{
              UIGraphicsBeginImageContextWithOptions(CGSizeMake(7.f, 1.f), NO, 0.0);
              CGContextRef c = UIGraphicsGetCurrentContext();
              [[self colorWithRed:185/255.f green:192/255.f blue:202/255.f alpha:1.f] setFill];
              CGContextFillRect(c, CGRectMake(0, 0, 4, 1));
              [[self colorWithRed:185/255.f green:193/255.f blue:200/255.f alpha:1.f] setFill];
              CGContextFillRect(c, CGRectMake(4, 0, 1, 1));
              [[self colorWithRed:192/255.f green:200/255.f blue:207/255.f alpha:1.f] setFill];
              CGContextFillRect(c, CGRectMake(5, 0, 2, 1));
              tableViewBackgroundImage = UIGraphicsGetImageFromCurrentImageContext();
              UIGraphicsEndImageContext();
          });
          return [self colorWithPatternImage:tableViewBackgroundImage];
      }
      
      @end
      

      此解决方案还允许调整背景的外观。随意更改绘图代码:)

      【讨论】:

      • 从技术角度来看不错的解决方案 ;-) 但是,我看到的问题是 Apple 在 iOS 6 中更改了分组表视图的背景外观。因此,此方法并未涵盖所有iOS 版本。即使它会在运行时检查 iOS 版本,它也不是面向未来的解决方案。
      • 必须包含 [tableViewBackgroundImage 保留];如果您直接在应用程序中处理内存分配。否则第二次访问新颜色时会崩溃。
      • @glsaza 你是对的,但是使用 ARC 它不会崩溃,因为静态变量默认是强的(__strong 并不是真正需要的,但更明确)。
      【解决方案4】:

      试试这个:

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

      【讨论】:

        【解决方案5】:

        首先,将其添加到您的 viewDidLoad

        self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableViewBackground.png"]];
        

        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableViewBackground.png"]];
        

        然后将此图片添加到您的应用中:

        tableViewBackground.png

        tableViewBackground@2x.png

        【讨论】:

        • 这也会映射到在 iPad 上使用它吗?我不记得图案是否会干净地重叠到侧面。如果是这样,这将非常有效(到处投票)
        • 哈哈,奇怪的是我在浏览 iOS 的 BNR 指南时遇到了这个问题。很高兴和你一起工作猫王。 :-)
        • 这些图像并不是 iOS 6 中当前图像的 100% 复制品。我的设计师几乎可以立即将它们识别为奇怪的。
        • 点击图片链接将我带到..blank页面..! :(如何获取图像..?
        • @Shailesh:它们有 1px 和 2px 高,请在你的应用中试试。
        【解决方案6】:

        如果它对任何人有帮助,这就是我在自定义视图中为获得此背景所做的具体操作(使用 Beloeuvre 先生的提示)

        - (void)viewDidLoad
        {
            [super viewDidLoad];
            self.view.backgroundColor = [UIColor clearColor];
            UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
            [self.view addSubview:tv];
            [self.view sendSubviewToBack:tv];
            // ...
        }
        

        【讨论】:

          【解决方案7】:

          对以前的 iOs 版本使用标准方法是个好主意。所以我改进了 James Boucher 的解决方案:

          + (void)setBackgroundColorForTableView:(UITableView*) tableView
          {
              UIColor* color = [UIColor whiteColor];
              NSString* version = [[UIDevice currentDevice] systemVersion];
              if([version floatValue] < 6.0)
              {
                  tableView.backgroundColor = color;
              }
              else
              {
                  tableView.backgroundView = nil;
                  UIView* bv = [[UIView alloc] init];
                  bv.backgroundColor = color;
                  tableView.backgroundView = bv;
                  [bv release];
              }
          }
          

          【讨论】:

            【解决方案8】:

            如果您使用故事板并且有很多视图,您可能很难找到视图。您可以单击右上角的“显示版本编辑器”按钮。这会将故事视图更改为 XML 文本视图。搜索“groupTableViewBackGroundColor”。您应该找到具有此属性的视图。

            【讨论】:

              【解决方案9】:

              详细说明@NSElvis 的解决方案,这里是相同的分组表格视图背景资源(它足够宽,因此您不会在横向上获得有趣的效果)

              back-tableview@2x.png

              要使用它,只需这样做

              [YOUR_VIEW setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"back-tableview"]]];
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2019-10-29
                • 1970-01-01
                • 1970-01-01
                • 2011-01-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多