views:

1505

answers:

3

I have a navigation based app where I push TableViewControllers onto the stack. I would like to add a background image to all of my TableViewControllers. Not a color, but an image. I know how I can do this using a Nib file and setting the table view itself to have use [UIColor ClearColor], but I don't want to go through all my TableViewControllers and change them to using Nib files, etc.

I also found this solution which would be great if I was just using a single tableviewcontroller in my app. I think there might be a way to make this work, by adding a subview "below" my table view that is created by default in a TableViewController?

Any suggestions would be great.

A: 

If your class is a UIViewController subclass then you can do it like this:

[self.view setBackgroundColor:
     [UIColor colorWithPatternImage:
      [UIImage imageWithContentsOfFile:
       [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
     @"background.png"]]]];
Madhup
Thanks, but that's actually the crux of my question. It is not a UIViewController - it is a UITableViewController.
hookjd
then you should follwo that tutorial.....
Madhup
what tutorial are you referring to?
hookjd
+5  A: 

It's a little different in a navigation based app: just change the background of the navigation view that each table view is sitting on. Placing the following code in viewDidLoad of each UITableViewController works:

self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]]; self.tableView.backgroundColor = [UIColor clearColor];

But you may only need to do it once, at the top level of the navigation controller, rather than in each tableview controller (although you'll still have to set each background to clear).

Gorm
That's exactly it. thanks very much.
hookjd
A: 

The answer given my Madhup is the correct answer. UITableViewController is a subclass of UIViewController, so adding that to your UITableViewController's viewDidLoad method works great.

Harkonian