15

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?

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The subviews property returns an NSArray, which does not respond to the -addSubview: method.
Good catch - fixed. You just need the addSubview method of a UIView.
14

As of iOS 3.2 the UITableView has a property called backgroundView. So instead of adding any subviews, just do this:

self.tableView.backgroundView = backgroundView; 

The table view will take care of resizing the background view to the size of the table. The background view will not move with the table cells. In other words, if the background view is an image view, the image will appear to stay locked on the screen with the cells gliding across it. The default table views also seem to have the smarts to make the appropriate elements of the cell clear automatically if there is a background view present.

2 Comments

Setting the backgroundColor using an image causes problems on the outer areas of each cell row. This works great.
For me in iOS7/8 if I set the backgroundView it covers the cells.
3

I would simply set the backgroundColor property of the UITableView itself. You can use UIColor's +colorWithPatternImage: method to convert a UIImage into a UIColor (that will repeat across the entire view, if not big enough):

// From within UITableViewController's -viewDidLoad: UIImage *image = [UIImage imageNamed:"yourImage.png"]; self.tableView.backgroundColor = [UIColor colorWithPatternImage:image];

Simply add yourImage.png and [email protected] to your application bundle, size it appropriately, and you're all set.

4 Comments

That results in something extremely strange-looking.
Seems to work only for spaces between cells. Table header, section headers, and space on either side of cells do not match the image.
It will render the color according to the visible space available, so the image looks "glued" to the top or bottom rows of the table.
Yeah, that no longer works in iOS 5.x. It adds color to cells only and when you scroll, the color goes away.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.