Hello,
How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
Thanks
Hello,
How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
Thanks
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.
[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"whatever.png"]]];
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.
We need to do something about that plain background. We're going to use a PNG image and display it behind the UITableView.
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:
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.
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;
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.