How to set a background image of a UITableView is a very often and common question. Today we will try to answer this.
There are two ways to solve this problem:
- Create a new UIView – lets call the view V – set the background image of V and embed a UITableView – lets call it TV – within V. Now set the background color of TV to
[UIColor clearColor]and do so for all UITableViewCells. Everything seems to work well but you have to handle some tasks (regarding the UITableView) on your own now like deselcting the currently selected UITableViewCell when returning from a prior pushed view. - The second approach requires less effort but creates a not so pretty side-effect. You can simply create a new UIView, set its background image and send this view to the back of the UITableView. If you do so the background image scrolls with the UITableView. So this solution should only be used if you do not have to many cells, i.e. to create a simple menu screen in a grouped style within your app.
For those guys, implementing the second solution, the following code would be helpful. Put it i.e. in your - (void) viewDidLoad; method or further initialization of your view controller.
//clear background color of uitableview self.view.backgroundColor = [UIColor blackColor]; //create new uiview with a background image UIImage *backgroundImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"your_background" ofType:@"png"]]; UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage]; //adjust the frame for the case of navigation or tabbars backgroundView.frame = CGRectMake(0, 0, 320, 460); //add background view and send it to the back [self.view addSubview:backgroundView]; [self.view sendSubviewToBack:backgroundView]; [backgroundView release];
One more thing I should not forget to mention is that iPhone SDK Documentation recommends not to use transparent images, table views or cells. This kind of stuff consumes a lot of resources, so use it wisely and only when necessary (I know, for a pretty user interface it is often indispensable
).
That’s all folks,
Andreas
Gr8 work my friend…
Thanks a lot, it was just what I was looking after the "overlapping sibbling warning".