Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a background image for a grouped table view?

I've seen some iPhone applications that use a custom image as the background for a grouped UITableView, instead of the standard gray lines.

How is this achieved?

like image 990
Bill Avatar asked Sep 03 '09 00:09

Bill


4 Answers

Here's what worked for me (and fairly simple once I figured it out ;)

1) Add a view in your app delegate and make it a subview of the window:

UIView *bgView = [[UIView alloc]initWithFrame:window.frame];

 bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"screenBG.png"]];
 [window addSubview:bgView];
 [bgView release];

2) On each view controller .m file, under ViewDidLoad, set background color of that particular view to transparent (so the other bgView created above will show through):

self.view.backgroundColor = [UIColor clearColor];

And in my case, the view controller in step 2 was a tableviewcontroller. Looks great.

And BTW, doing the following in each view controller did NOT work well:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"screenBG.png"]];

So follow steps 1 and 2 above.

Hope this helps out, Tbone

like image 142
tbone Avatar answered Jan 05 '23 11:01

tbone


Try this

- (void) viewDidLoad {
    [super viewDidLoad];

    self.tableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"wallpaper.png"]];
    self.tableView.opaque = NO;
}
like image 40
John Johnson Avatar answered Jan 05 '23 13:01

John Johnson


In another project (developed using 2.2.1) I did this by setting my UITableView's background opacity to 0%, and then simply layering a UIImageView behind it using Interface Builder. This allowed me to have a fixed background regardless of the table state. You can also set the background of the UITableView to be an image instead, but then the background scrolls with the table. (I don't have the code handy at the moment, but I got the tip a while back on the Apple developer forums).

Note that this can cause some performance issues. Apple discourages using transparency whenever possible because the GPUs on the pre-3GS models aren't particularly beefy.

like image 32
Shaggy Frog Avatar answered Jan 05 '23 11:01

Shaggy Frog


You can use the +[UIColor colorWithPatternImage:(UIImage)] method like so:

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
like image 38
Garrett Avatar answered Jan 05 '23 11:01

Garrett