3

Hey everyone, this seems like it should be a simple one; I really hope it is. As always, thanks in advance!

All I'm trying to do is add a background image to a grouped style tableview. I'm using the following method in viewDidLoad:

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];

Unfortunately, background.png seems to be drawn not only behind the cells, but also on the backgrounds of the cells and the headers as follows:

alt text http://www.freeimagehosting.net/uploads/194449ffc1.png

I know there are a few questions on this site addressing similar issues, but I'm afraid I couldn't get anything to work. How can I set the background of UITableView (the tableview style is "Grouped") to use an image? led me to add

self.tableView.opaque = NO;
self.tableView.backgroundView = nil;

to viewDidLoad and to add

cell.backgroundView = nil;
cell.opaque = NO;

in configuring my cells. Needless to say, it didn't work. Adding

cell.backgroundColor = [UIColor clearColor];

didn't work either. And sadly, that was my last idea. I'd really like to be able to do this without adding the background in interface builder, if possible. I rather like doing as much as I can programmatically. I hope asking that isn't too greedy. Thanks again!

* * * EDIT: * * *

I came across another approach to accomplishing this, but ran into another problem. I added the following code to viewDidLoad:

    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.tableView.window addSubview:bgView];

    [self.tableView addSubview:bgView];
    [self.tableView sendSubviewToBack:bgView];

It looked promising, and it likely is, but unfortunately in gave me the following: alt text http://www.freeimagehosting.net/uploads/c02dd68e20.png

It looks as though it either wasn't sent to the back and the headers somehow show through or the cells went to the back with it. I'm not at all sure. Any advice here would be appreciated as well.

Here's where I found the code for the edit: Add UIView behind UITableView in UITableViewController code

Community
  • 1
  • 1
Arthur Skirvin
  • 1,210
  • 1
  • 12
  • 14

5 Answers5

4

I went through a similar path to what you describe here. I finally found an answer that worked for me on the following post:

How to set the background of a UITableView to an image?

It was two lines of code posted by user rkb. Here's the code:

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];
Community
  • 1
  • 1
dana_a
  • 95
  • 1
  • 2
3

Another solution if nothing else here is working out. In ViewDidLoad:

UIImage *background = [UIImage imageNamed:@"MyBackground.png"];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:background];

This will place your static (doesn't stretch or move) background behind all of your grouped tableview elements, such as the headers and cells.

SethHB
  • 743
  • 4
  • 12
2

Using the previous responses(particularly from dana_a) this is how I got the same thing to work

self.tableView.backgroundColor = [UIColor clearColor]; self.parentViewController.view.backgroundcolor = [UIColor colorWithPatternImage:[UIImge imageNamed:@"bgtest2.png"]];

This is in my viewWillAppear: method in for my case a detail view controller with a grouped tableview.

jpweber
  • 124
  • 1
  • 2
1

I placed this code in my ViewDidLoad method and it worked beautifully.

self.termsTableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"metal4.png"]];
Tim L
  • 170
  • 6
0

I still haven't figured out why the above doesn't work or how to modify it so that it does, but by adding the subView to the window in the App Delegate instead of the tableView here I was able to get it to work.

Instead of just getting it to function and moving on I'd like to actually learn, so I'm still curious as to where I went wrong above. As such, I'll leave this as unanswered for a time and if you can tell me my error(s) I'll give you the up vote and the green check mark. Thanks!

Arthur Skirvin
  • 1,210
  • 1
  • 12
  • 14