56

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

Linger
  • 14,942
  • 23
  • 52
  • 79
ebaccount
  • 5,051
  • 11
  • 43
  • 47

11 Answers11

155

In newer versions of the SDK, you'll need to set tableView.backgroundView if you want it to be transparent, try something like this:

tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
Sam Soffes
  • 14,831
  • 9
  • 76
  • 80
  • 1
    Thanks! You don't know how much trouble I was having with this. I was missing the tableView.backgroundView = nil line! – Mark Oct 29 '10 at 01:07
  • Thanks, I put up a bit of code and a post about this: http://paulsolt.com/2011/05/transparent-uitableview-with-custom-background-uiview-and-tap-gestures/ – Paul Solt May 18 '11 at 19:52
  • This method works but it inserts the background to the ViewController, instead i need to add a background just to the content of the TableView (I don't want the background to be inserted behind the header or the footer of the TableView) – Heysem Katibi Oct 27 '16 at 09:37
18

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.

leviathan
  • 11,080
  • 5
  • 42
  • 40
12
[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"whatever.png"]]];
Lounges
  • 4,674
  • 5
  • 32
  • 43
  • 5
    It works. But in a grouped table, the sides of the cell are not transparent. I have already tried setting backgroundView.opaque=NO, nothing happens. – leolobato Aug 13 '09 at 14:36
9

tableView.backgroundView = nil; is enough. No need to set background color as Clear Color.

Bharathi
  • 485
  • 6
  • 16
2

In UI Builder the Background color has an "Other" choice. This brings up a color picker. The color picker has an opacity setting. If you set the Opacity of the COLOR to 0 it works, can't speak to performance.

hasane
  • 21
  • 1
2

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.

pix0r
  • 31,139
  • 18
  • 86
  • 102
1

What I've found is that you have to use a "plain" styled table with a transparent background and then recreate the look of the rounded-corner cells by setting each cell's backgroundView to a UIImageView with a image that simulates the rounded look. This means that the top, bottom, and middle cells need different background images.

However, this does not address what happens when the user taps the cell and it goes "highlighted" - it will look squared off then. You can get around this by setting the highlighted image for your faked tablecell background image. You will also want to create your own disclosure accessory view (ImageView) with a white highlighted version. Then you can create a cell like this one I'm using (below). After I alloc one of these cells I then set the backgroundView and accessoryView to my UIImageViews.

#import "ClearBackRoundedTableCell.h"


@implementation ClearBackRoundedTableCell

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier 
{
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
    }
    return self;
}


-  (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if( [[self.accessoryView class] isSubclassOfClass:[UIImageView class]] )
        ((UIImageView *)self.accessoryView).highlighted = highlighted;

    if( [[self.backgroundView class] isSubclassOfClass:[UIImageView class]] )
        ((UIImageView *)self.backgroundView).highlighted = highlighted;

    self.textLabel.highlighted = highlighted;
}

@end

One note if you go this route: the cells in a grouped table are typically 300 px wide (in portrait mode) but your plain table here would need to be 302 wide to allow for the grey line on each side of the table, which is normally outside of the "content" of the table cell.

Jason
  • 1,323
  • 14
  • 19
  • Better than using an image for the background view, do the drawing yourself (core graphics makes it easy to draw a rounded rectangle with a border) – Johnus Oct 21 '10 at 06:42
  • `[[something class] isSubclassOfClass:[UIImageView class]]` is the same as `[something isKindOfClass:[UIImageView class]]` – user102008 Jul 29 '11 at 23:58
1

After spending a while with color picker, I found out that you need to specify opaque background not for the table view cell xib, but for the Table View where the cells will be located, which is another xib. From what I have seen, table view cell background attributes have no visual effect.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

try this one

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;

It worked for me in grouped tableview.

user431791
  • 341
  • 2
  • 17
0

Make UITableview background as clear color.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
0

Programmatically you can do it like this if your image is added into your resources:

self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
UIImage *backroundImage = [UIImage imageNamed:@"my_backround"];
UIImageView *backroundImageView = [[UIImageView alloc] initWithImage:backroundImage];

Else you can do it in Interface Builder with this style :

IB tableview with image behind

You may need to configure the header files interface from UITableViewController to UIViewController and add <UITableViewDataSource,UITableViewDelegate> ,also don't forget to set the attributes of the tableview to not be opaque and reconnect the tableviews datasource and delegate outlets to the viewcontroller.

AMAN77
  • 6,218
  • 9
  • 45
  • 60