36

I was just testing my app with iOS 6.0 and Xcode 4.5GM and I have set up a view like this:

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

So, the view has the same pattern than a common table view.

This works fine on iOS 4 and 5, but in iOS 6 it just gives me a white background.

Is this deprecated? If so, how can I replace it?

Thanks

estemendoza
  • 3,023
  • 5
  • 31
  • 51

9 Answers9

31

This method will be deprecated during the 6.0 seed program If you want to have a background in your own view that looks like the table view background, then you should create an empty table view and place it behind your content.

dhcdht
  • 463
  • 4
  • 8
25

First, add this to your viewDidLoad:

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

OR

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

Then add this images to your app:

tableViewBackground.png

tableViewBackground.png

tableViewBackground@2x.png

tableViewBackground@2x.png

3lvis
  • 4,190
  • 1
  • 30
  • 35
18

I've written a UIColor category to replace 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

This solution also allows to tweak the appearance of the background. Feel free to change the drawing code :)

Felix
  • 35,354
  • 13
  • 96
  • 143
  • Nice solution from a technical point of view ;-) However, the problem I see with this is that Apple has changed the background appearance of grouped table views with iOS 6. Thus, this method covers not all iOS versions. And even if it would check at runtime the iOS version, it wouldn't be a future-proof solution. – Tafkadasoh Oct 24 '12 at 09:47
  • You must include [tableViewBackgroundImage retain]; if you are handling memory allocations directly in your application. Otherwise it will crash the second time you access the new color. – DDRider62 Oct 26 '12 at 18:17
  • @glsaza you're right, but with ARC it won't crash because static variables are strong by default (the __strong is not really needed, but more explicit). – Felix Oct 26 '12 at 19:28
13

In iOS6 SKD, comments in UIInterface.h suggest the following:

Group style table view backgrounds can no longer be represented by a simple color. If you want to have a background in your own view that looks like the table view background, then you should create an empty table view and place it behind your content.

This method will be deprecated during the 6.0 seed program

A simple solution is to set the background with equivalent RGB values:

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

You can them set the view background to color to White or whatever you like in the xib to suppress the warning.

Community
  • 1
  • 1
6

If it helps anyone, here's specifically what I'm doing in my custom view to get this background (using hint from Mr 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];
    // ...
}
James Boutcher
  • 2,593
  • 1
  • 25
  • 37
2

Try this:

[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[[UIView alloc] init] autorelease]];
Ravi Sharma
  • 975
  • 11
  • 29
1

You may have difficult time to locate the view if you use storyboard and have many views. You can click on "Show the Version editor" button the right top corner. This will change story view to XML text view. Search for "groupTableViewBackGroundColor". You should find views with this attribute.

enter image description here

David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
0

It's good idea to use standard methods for previous iOs versions. So I improved solution of James Boutcher:

+ (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];
    }
}
0

Elaborating on @NSElvis's solution, here is the identical grouped table view background asset (it's wide enough so you don't get funny effects in landscape orientation)

back-tableview@2x.png

To use it, simply do

[YOUR_VIEW setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"back-tableview"]]];
Ege Akpinar
  • 3,266
  • 1
  • 23
  • 29