Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting background Image to uitableview?

I want to set an image as the background for my uitableview. This is the code I am using:

 self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_tableview.png"]];

This is showing the image right but as soon as the table is populated something weird happens. Parts of images are shown in each tableviewcell and also the image scrolls along with the table. How can I make it so that the image remains fixed and that each tableviewcell doesn't show part of the image?

Thanks,
TC

like image 606
Tushar Chutani Avatar asked Jul 03 '11 20:07

Tushar Chutani


2 Answers

You don't want to set the background of the UITableView if you don't want it to scroll. Instead, you want to set the background of the UITableView to be transparent - and provide your own fixed background behind the UITableView.

To do this, first set the background of the UITableView to "Clear Color".

Programmatically: self.tableView.backgroundColor = [UIColor clearColor];

You'll also want to un-check the opaque checkbox.

Programmatically: self.tableView.opaque = NO;

Setup the fixed background you want in the view behind the UITableView.

Make sure any sub-views in any of your UITableViewCells are also backgroundColor = [UIColor clearColor]; (UITextViews, etc.)

Example Xcode 4 / Interface Builder setup

like image 76
Steve Avatar answered Sep 22 '22 02:09

Steve


Image for the TableView background

self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"your_image"]];

Do not forget write the file name with the image's type. EXAMPLE:

self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myPhoto.png"]];
like image 34
user4006556 Avatar answered Sep 20 '22 02:09

user4006556