Add background image to UITableViewController in app navigation

I have a navigation based application that I push UITableViewControllers onto the stack. I would like to add a UImage background to all my UITableViewControllers . Not UIColor , but UImage . I know how to do this using a Nib file and setting the UITableView itself to use [UIColor ClearColor] , but I donโ€™t want to go through all my UITableViewControllers and change them to Nib files, etc.

I also found this solution , which would be great if I just used one desktop controller in my application. I think there may be a way to make this work by adding a โ€œbelowโ€ view of my table view, which is created by default in the UITableViewController ?

Any suggestions would be great.

+7
iphone uitableview uiimage uinavigationcontroller
source share
6 answers

This is slightly different in a navigation-based application: just change the background of the navigation view that each table view sits on. Placing the following code in the viewDidLoad each UITableViewController works:

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

But you may need to do this only once at the top level of the navigation controller, and not in each table controller (although you still have to set each background to clear).

+48
source share

On iOS6, use this:

 UIImageView *boxBackView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TextureBoxboard.jpg"]]; [self.tableView setBackgroundView:boxBackView]; 
+16
source share

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

 [self.view setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageWithContentsOfFile: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"background.png"]]]]; 
+2
source share

The answer to my madhup is the correct answer. UITableViewController is a subclass of UIViewController, so adding this UITableViewController viewDidLoad method works fine.

+1
source share
 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

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

+1
source share

Starting with iOS 3.2, there is a [UITableView setBackgroundView:], which may be simpler than some other proposed solutions in the future.

+1
source share

All Articles