Easy Custom UITableView Background

It's embarrassing that it took me this long to figure it out, but there is an easy way to put an image behind a UITableVIew controlled by a UITableViewController.

Searching for info on how to give a grouped table view a custom background will usually only tell you half the story. They tell you how to make the background transparent. They then tell you to put a UIImageView behind it. Great, thanks. How do I do that without creating a view controller hierarchy (an evil view controller hierarchy) and a whole bunch more work for myself, just for a custom background?

Easy stupid:

// create the view controller from your nib
MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
vc.tableView.backgroundColor = [UIColor clearColor];

// create the background
UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.png"]];
iv.contentMode = UIViewContentModeCenter;
iv.userInteractionEnabled = TRUE;

[navigationController pushViewController:vc animated:YES];

// put the background behind the tableview
[vc.tableView.superview addSubview:iv];
[iv addSubview:vc.tableView];

// don't forget to release your view controller & image view!
[vc release];
[iv release];

Once you've created your table view controller you just need to put the image view in the position that the table view was and make the table view a subview of the image view.

You could probably make this method part of the viewWillAppear: method, for more automatic-ness.