UIColor groupTableViewBackgroundColor, question


#1

Ok, I realize this may be answered somewhere else but on current iOS(6.0) the UIColor as mentioned in the title is not showing up for me, on my iPhone(s), iPad(s), or the simulators. I tested BNR’s Solution it also doesn’t work for this (I realize its minute for the learning, but not for a production level item) . I then tested some stub code from coreRecipe’s they all seem to be falling flat on there face for accomplishing this. I then tried a different computer just to make sure I wasn’t insane… is anyone else having this issue or have a workaround for it?


#2

Ok so I did a bit of self driven research it seems this was deprecated in the recent changes. This is not very expressly stated yet. I say yet because the documentation is giant. However it was mentioned expressly in story boarding. Incidentally I ran across it for a personal project.

The easiest solution I found was to just make them both iPad color style for aesthetics and do demo color to demonstrate the difference for later chapters…


#3

If you are asking how to change the background colour when using UITableViewStyleGrouped, I have done this:

[[self tableView] setBackgroundView:nil]; [[self tableView] setBackgroundColor:[UIColor colorWithRed:199.0f/255.0f green:195.0f/255.0f blue:171.0f/255.0f alpha:1]];

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#4

My solution for depreciated groupTableViewBackgroundColor:
download zip with “tableViewBGStrip.png” and "tableViewBGStrip@2x.png"
dl.dropbox.com/u/78439335/table … images.zip

  • Add both images to the project.
    In DetailViewController.m

- (void) viewDidLoad { UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableViewBGStrip.png"]]; [self.view setBackgroundColor:bgColor]; }
If downloading images does not work, this is how i created the images:

  • Run the app in the simulator in iPhone mode
  • Set window to 100%
  • Take screenshot of the tableView without items
  • Open screenshot in Preview, select a horizontal strip of 320 X 10 in the middle of the image, copy, select new from clipboard
  • Save as "tableViewBGStrip.png"
    do the same with iPhone Retina(4 inch) mode, 640 x 20, save as “tableViewBGStrip@2x.png”
  • Add both to project etc. as above

#5

Weird thing that it was deprecated in iOS 6 but this info is not in the ios 6.1 docs in xcode, so I didn’t get from the start why it’s not working.
Anyway, here is a solution I made after extensive google search:

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

What it does is sets main bgcolor to transparent, then adds another view with grouped bg, then we move it to back.


#6

[quote=“titicaca”][code]

  • (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];
    }
    [/code][/quote]
    This looks great. However, the TableView catches touches and has no delegate. (It broke my code that dismissed the keyboard by tapping on the background.)

To fix this, add the line:

and then touches on the background will be processed by the transparent superview.


#7

If you CMD click groupTableViewBackgroundColor you’ll get:

// 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.

  • (UIColor *)groupTableViewBackgroundColor; // This method will be deprecated during the 6.0 seed program

If you’re using a XIB, you can just drag the this on your view as well after you’ve finished setting up everything.


#8

[quote=“titicaca”]Weird thing that it was deprecated in iOS 6 but this info is not in the ios 6.1 docs in xcode, so I didn’t get from the start why it’s not working.
Anyway, here is a solution I made after extensive google search:
What it does is sets main bgcolor to transparent, then adds another view with grouped bg, then we move it to back.[/quote]

I tried implementing this, but I found when I rotated the device, the tableview was breaking. And when I ran it on the iPad, the tableview was the size of the iPhone.

There may be a more elegant fix, but the one I came up with was:

  1. set tv as an instance variable
  2. update the frame to: initframe: [[self parentViewController] view].bounds
  3. Implement:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [tv setFrame:[[self parentViewController]view].bounds];
}

Adding a grouped tableview to the XIB worked for all formats and rotations, but makes the xib harder to read when adjusting.


#9

Instead of having to register the UITableView as a ivar, you can use the autoresizingMask property to control its behavior when its parent view’s frame changes. To continue the earlier example:

[code]- (void) viewDidLoad
{
[super viewDidLoad];
[[self view] setBackgroundColor:[UIColor clearColor]];
UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
[tv setUserInteractionEnabled:NO];

// Setting the flags below will cause the view to resize both its width and height.
[tv setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

[[self view] addSubview:tv];
[[self view] sendSubviewToBack:tv];
}[/code]

Certainly, an ivar is useful if you need to manipulate the background table view in other ways, but since we are using it just for stylistic reasons, adjusting the values of the autoresizingMask property will work just as well, with less code.