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

Iphone

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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;

Solution 2 - Iphone

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.

Solution 3 - Iphone

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

Solution 4 - Iphone

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

Solution 5 - Iphone

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.

Solution 6 - Iphone

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.

Solution 7 - Iphone

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.

Solution 8 - Iphone

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.

Solution 9 - Iphone

try this one

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

It worked for me in grouped tableview.

Solution 10 - Iphone

Make UITableview background as clear color.

Solution 11 - Iphone

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionebaccountView Question on Stackoverflow
Solution 1 - IphoneSam SoffesView Answer on Stackoverflow
Solution 2 - IphoneleviathanView Answer on Stackoverflow
Solution 3 - IphoneLoungesView Answer on Stackoverflow
Solution 4 - IphoneBharathiView Answer on Stackoverflow
Solution 5 - Iphonepix0rView Answer on Stackoverflow
Solution 6 - IphonehasaneView Answer on Stackoverflow
Solution 7 - IphoneJasonView Answer on Stackoverflow
Solution 8 - Iphone18446744073709551615View Answer on Stackoverflow
Solution 9 - Iphoneuser431791View Answer on Stackoverflow
Solution 10 - IphoneAbdul YasinView Answer on Stackoverflow
Solution 11 - IphoneAMAN77View Answer on Stackoverflow