views:

24

answers:

2

Hi, I am using this code to add a background image to my tableview

myTable.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]] autorelease];

on the internet they say this code works, but it's not working for me can someone help me please?

A: 

if its a UITableViewController, as I believe it is, do this:

[myTable setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];

or you can do (on a non-table view controller)

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];

or on viewDidLoad you can do:

UIImageView *backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]] autorelease];
[self.view addSubview:backgroundView];

but thats a bit messy - I prefer to use setBackgroundColor:

Thomas Clayson
is it possible to do this in the appdelegate? so things wouldn't be messy at all
Nico
no, unfortunately its going to be messy
Thomas Clayson
but you can set the background of the navigation controller `self.navigationController.backgroundColor = ...` and then make every subsequent view transparent? bit of a faff though.
Thomas Clayson
A: 

The problem is that backgroundview for tableview is a UIView, not a UIImage. So you need to create a view, easiest way is to use a UIImageView. For example this would put your splash screen image as the background view for the table in a UITableViewController...

    UIImage *backImage = [UIImage imageNamed:@"default.png"];
UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage];
self.tableView.backgroundView = backImageView;

The methods mentioned above are pre the introduction of tableview property backgroundview in 3.2

edit - dear god I was obviously having a coffee deficit today :) The op is indeed using a UIImageView! The only thing I can think is that he is targeting a pre 3.2 platform...

Colm B