tags:

views:

327

answers:

5

Hello all,

I am using a UITableViewController which uploads a table. I have a Nib File with UITableView in it.Now I want to set the background of the tableView either from interface builder or from the TableViewController to an image.

How to do that.

OK so I created an UIImage in my controller. Now when I add where do I need to add it.

When I try adding it and set the color of tableView to clearColor, it just shows me the Image and not the table although I make sure that image is being sent to back of all views.

Guys Please note that I am not dealing a UIView and adding a tableView as its subview But I am dealing with a UITableView .

A: 

What you can do is set the backgroundColor property of the tableview to [UIColor clearColor] and have a UIImageView of the same size and position underneath your TableView to show your background.

Brad Smith
+5  A: 

Place a UIImageView behind the UITableView, then do this:

tableView.backgroundColor = [UIColor clearColor];
Chris Newman
Also make sure that the UITableViewCells that you're using (as well as their subviews) have transparent backgrounds.
Justin Gallagher
A: 

TableView properties include background color. Set this to clearColor; and put the image behind it. (In IB I think you'll have to delete the tableview add an image and then add your tableview back again)

if you're subclassing UITableViewController you get a tableview for free, no ivar or nib required, however since you're using a background I would stick with IB.

JoePasq
+5  A: 

Somehow playing around I was able to find out a way.

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];
rkb
This works very well. Note that the image from the file will be tiled if it is not as big as the view, so if your image repeats you can reduce the image size substantially.
Amagrammer
A: 

The backgroundColor = [UIColor colorWithPatternImage: image] method rkbang outlines works great. Here is another method to achieve the same effect by adding a new view to the parentViewController. This would be useful if you want to mask other contents the parentViewController might have.

In your UITableViewController subclass, in -viewDidLoad insert:

//make a cool background image
self.tableView.backgroundColor = [UIColor clearColor];
UIImage *patternImage = [UIImage imageNamed:@"backgroundImage.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];

[self.parentViewController.view addSubview:backgroundImageView];
[self.parentViewController.view sendSubviewToBack:backgroundImageView];
//release the background image and image view (the backgroundImageView is still retained by the parentViewController)
[patternImage release];
[backgroundImageView release];

You may wish to keep track of the backgroundImageView so you can remove it or replace it. The code example above leaves it to the parentViewController to manage the new view. If you're loading and unloading this UITableView, you'll be leaking these UIImageViews with every load unless the parentViewController is releasing them at the same rate somehow.

John M. P. Knox