Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Fixed-Position UITableView Background

I'm building an iPhone app without the use of Interface Builder. I have set the background of a grouped UITableView in the following manor:

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

I'm trying to fix this background image so that it doesn't scroll with the table cells. Does anyone know how to do this?

like image 390
treblig Avatar asked Dec 25 '09 06:12

treblig


4 Answers

We find that it works exactly as you ask, if instead of using backgroundColor, you assign to backgroundView, like so:

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

The table cells then scroll on top of the background, which is stationary. (This has been available since version 3.2 of the SDK, I believe.)

like image 62
Joe Strout Avatar answered Nov 08 '22 19:11

Joe Strout


You can achieve a stationary background using a pattern image as follows:

UIView *patternView = [[UIView alloc] initWithFrame:tableView.frame];
patternView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"groupedBackground.png"]];
patternView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.backgroundView = patternView;
like image 44
titaniumdecoy Avatar answered Nov 08 '22 21:11

titaniumdecoy


A little late but maybe for all the others looking for a solution. I could get that work by calling the parentViewController in the viewDidLoad method of the UITableViewController:

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

or with using just a background color:

self.parentViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.tableView.backgroundColor = [UIColor clearColor];

It took me a while to figure that out but now it works like a charm.

like image 28
JFS Avatar answered Nov 08 '22 20:11

JFS


You can place an additional view below UITableView and set its background, so if UITableView is transparent - you'll achieve your goal - it will have correct background and it will not scroll.

like image 32
Valerii Hiora Avatar answered Nov 08 '22 20:11

Valerii Hiora