Iphone – Add UIView behind UITableView in UITableViewController code

backgroundcocoa-touchiphoneuitableview

I would like to use a fixed image as the background in a simple grouped table view in my iPhone program. Unfortunately, no matter what I do, the background is always solid white. I have followed several supposed solutions on this site and others to no avail. Here is the relavant code in the viewDidLoad method of the table view controller class (note, this code uses a solid blue color rather than an image for simplicity's sake):

self.tableView.opaque = NO;
self.tableView.backgroundColor = [UIColor clearColor];
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
backgroundView.backgroundColor = [UIColor blueColor];
[self.tableView.window addSubview:backgroundView];
[backgroundView release];

I suspect that I am not positioning the backgroundView view in the right place. I have tried sendToBack:, bringToFront:, and others but I always just get a white background. Is it possible to do this from within the UITableViewController? Must I use Interface Builder?

Best Answer

Use UITableView's subviews property to add your background there:

[self.tableView addSubview:backgroundView];
[self.tableView sendSubviewToBack:backgroundView];

Also, your cell/header etc. will probably need to be set to transparent for your backgroundView to be visible.