7

I have a navigation based app where I push UITableViewControllers onto the stack. I would like to add a background UImage to all of my UITableViewControllers. Not a UIColor, but an UImage. I know how I can do this using a Nib file and setting the UITableView itself to have use [UIColor ClearColor], but I don't want to go through all my UITableViewControllers 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 UITableViewController?

Any suggestions would be great.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
hookjd
  • 117
  • 1
  • 1
  • 7

6 Answers6

48

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).

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Gorm
  • 2,714
  • 1
  • 18
  • 12
  • In addition I needed a 3rd line: self.view.backgroundColor = [UIColor clearColor]; – pulkitsinghal May 17 '12 at 17:49
  • 2
    This doesn't push the background image down to the bottom of a nav bar like a viewcontroller does... – zakdances May 22 '12 at 21:19
  • I am using this in my tableVC: [self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]]]; works like a charm ;) – raistlin Sep 06 '12 at 10:39
  • New question here: http://stackoverflow.com/questions/12520585/how-do-i-add-background-image-to-a-grouped-style-uitableviewcontroller-in-ios-6 – Steve Moser Sep 20 '12 at 20:41
  • @SteveMoser I added the iOS Solution to this question – bitboxer Sep 27 '12 at 12:28
16

On iOS6 use this:

UIImageView *boxBackView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TextureBoxboard.jpg"]];
[self.tableView setBackgroundView:boxBackView];
bitboxer
  • 544
  • 6
  • 17
  • why iOS6? `@property(nonatomic, readwrite, retain) UIView *backgroundView NS_AVAILABLE_IOS(3_2); // the background view will be automatically resized to track the size of the table view. this will be placed as a subview of the table view behind all cells and headers/footers. default may be non-nil for some devices.` – Artem Shmatkov Sep 29 '12 at 19:57
  • Yes, I know. But the other solution that is mentioned here is not working on iOS6 and this solution was buggy on < 4 I think. I tried it before, but it did not work. Don't know when this exactly was fixed tho. – bitboxer Sep 30 '12 at 00:04
2
UIImageView *backgroundView = 
  [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
backgroundView.frame = CGRectMake(0, 
                                  0, 
                               self.navigationController.view.frame.size.width, 
                               self.navigationController.view.frame.size.height);
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
                                  UIViewAutoresizingFlexibleHeight;
[self.navigationController.view insertSubview:backgroundView atIndex:0];
[backgroundView release];
self.tableView.backgroundColor = [UIColor clearColor];

As Gorm said

only need to do it once, at the top level of the UINavigationController

mmBs
  • 8,421
  • 6
  • 38
  • 46
snuc
  • 31
  • 3
2

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 Singh Yadav
  • 8,110
  • 7
  • 51
  • 84
1

As of iOS 3.2, -[UITableView setBackgroundView:] exists, which may be easier than some of the other proposed solutions going forward.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Rob Rix
  • 358
  • 1
  • 8
1

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.

memmons
  • 40,222
  • 21
  • 149
  • 183