7

There are currently a lot of answers on how to set a custom fixed background for a UITableView. But is there any trick to make the background image scroll together with the table cells?

Thanks!

Stefano Verna
  • 642
  • 9
  • 23

5 Answers5

7

You have to set the background image via backgroundColor property of UITableView:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
northsea
  • 152
  • 1
  • 8
  • 2
    If it's not an `UITableViewController` you have to use `tableView.backgroundColor = ...` – Jochen Sep 24 '12 at 13:45
  • 1
    Does this also work in iOS6? I try this in my viewDidLoad but nothing happens it still shows the standard grouptableview bg. – JonEasy Apr 17 '13 at 13:41
3

.

UIImageView *background = [[UIImageView alloc]initWithImage: [UIImage imageNamed: @"background.jpg"]];
    [tableView addSubview: background];
    [tableView sendSubviewToBack: background];
oxigen
  • 6,263
  • 3
  • 28
  • 37
  • 1
    Thanks, tried it, but I think the sendSubviewToBack message doesn't work as it should in this case: the UIImageView gets positioned above the cells. Any clues? – Stefano Verna Jul 13 '09 at 14:06
  • 1
    This approach was my first try. Sending it to the back still puts it above the cells though. – Sam Soffes Nov 01 '10 at 18:42
3

I would think that the answer is to subclass the UITableView and override layoutSubviews. In the UITableView you should make a property that points to a background UIImageView.

When you override layoutSubviews just write

-(void) layoutSubviews {
    [self sendSubviewToBack: backgroundImageView];
    [super layoutSubviews];

}
thewormsterror
  • 1,608
  • 14
  • 27
  • Sadly, this is not enough to fix all corner cases where views shuffle. – steipete Sep 01 '11 at 09:58
  • how so? it actually does solve the problem, I tried it myself. What corner cases are you talking about? – thewormsterror Sep 08 '11 at 23:41
  • 1
    Sometimes, UITableView shuffles views. It's not deterministic, and different between iOS versions. Don't use this. I used a scrolling UIImageView that's re-positionized in scrollViewDidScroll – steipete Sep 09 '11 at 08:36
0

This answer could also be helpful. Background image that's scrollable and in synch with the UITable scrolling

It involves a bit of trickery with Table Views.

You have to create a separate background table that is a slave of your foreground table, and it uses KVO to listen to the foreground table view's contentOffset property.

Community
  • 1
  • 1
horseshoe7
  • 2,745
  • 2
  • 35
  • 49
-6

A possible answer was -- obviously -- already on stackoverflow:

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

Ref: iPhone - Setting background on UITableViewController

Edit: That's not a perfect answer, I know :)

Community
  • 1
  • 1
Stefano Verna
  • 642
  • 9
  • 23
  • 2
    That will use parts of the background in each cell. So if you have background image higher then one cell that won't work. – OgreSwamp Jul 19 '10 at 20:49
  • 6
    This is NOT a correct answer because the background image will not scroll together with the table. – Bobo May 29 '11 at 16:07
  • 1
    Is this supposed to work? I am facing the same problem which @OgreSwamp mentioned, though the background is scrollable with the tableview the cell uses parts of the background in each cell. If this solution does not work, why was this accepted as correct answer? – Raj Pawan Gumdal Mar 19 '12 at 10:35