4

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 .

rkb
  • 10,933
  • 22
  • 76
  • 103

7 Answers7

5

Place a UIImageView behind the UITableView, then do this:

tableView.backgroundColor = [UIColor clearColor];
Chris Newman
  • 2,240
  • 2
  • 24
  • 30
4

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
  • 10,933
  • 22
  • 76
  • 103
  • 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 Oct 21 '09 at 22:17
  • Accessing parentViewController.view is a terrible idea - you don't know what kind of backing it has, and it's a pretty bad violation of encapsulation. Even if it works now, the next iOS update may totally break your code. Don't do this. – Michael Tyson Mar 29 '11 at 12:29
2

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"parentViewBackground.png"]];

the image size should be
2x - 640*960
1x - 320*480

the size smaller than above, it will be tiled.

damithH
  • 5,148
  • 2
  • 27
  • 31
  • I added a `UIImageView` in back behind my `UITableView`, and in the `viewDidLoad` set `backgroundColor` to clear. Works great! Thank you damithH – Jay Imerman Dec 21 '12 at 17:01
1

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
  • 713
  • 4
  • 11
0

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 The App Guy
  • 16,255
  • 2
  • 41
  • 60
0

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.

fearmint
  • 5,276
  • 2
  • 33
  • 45
0

You can use the table-view's backgroundView. This worked for me.

[self.tableView setBackgroundView: [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"????.png"]]];

where ????.png is your background image.

mobibob
  • 8,670
  • 20
  • 82
  • 131