How can I set the background of UITableView (the tableview style is "Grouped") to use an image?

Viewed 65948

How can I set the background of UITableView (the tableview style is "Grouped") to use an image?

11 Answers

We need to do something about that plain background. We're going to use a PNG image and display it behind the UITableView.

  1. Prepare a PNG image. It should be either 320x460 (if you have the status bar visible in your app) or 320x480 (if you hide it).
  2. Drag it into XCode into the Resources folder and add to your project
  3. Load the NIB file containing your UITableView into Interface Builder
  4. Open the library (Tools> Library), switch to the Media tab, and drag the image to the View, create a new UIImageView.
  5. Use the inspector to move and resize the image so it's at X=0, Y=0, Width=320, Height=480
  6. Put the UIImageView behind the UITableView (Layout > Send to Back)
  7. Save, Build and Go!

Disappointingly, you won't be able to see your background. The UITableView's background is blocking us from seeing the UIImageView. There are three changes you need to make:

  1. In the Attributes Inspector, make sure the UITableView's "opaque" checkbox is unchecked!
  2. Set the UITableView's background color to transparent:

    tableView.backgroundColor = [UIColor clearColor];

I hope this helps and solves your problem. It has worked for me and I have yet to find a more elegant way to display a background image for a UITableView.

The advantage of my solution, in comparison with setting a background image directly on the UITableView, is that you can indent the table's content. I often wanted to do this to just show two or three table cells at the bottom of the screen.

[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"whatever.png"]]];

One way would be to make the table view transparent (set the view's background to 0% opacity) and place a UIImageView behind the UITableView. Remember that transparent tables and table cells will not perform as well as opaque ones.

Related