14

Is it possible to have a background image used behind a section in a UITableView?

I can't think of how to achieve this.

4
  • it seems like what you want to do would be possible either with layers or by using subviews. Can you post a mockup of what you're looking for? Commented Jul 29, 2012 at 15:27
  • I'll try and get an image of what I need uploaded soon! Normally you would have a background on each cell, which could be a colour, or an image. However, I want a single background (In this case an image) dropped behind all the cells in a single section of my table. Commented Jul 29, 2012 at 15:32
  • I understand. There is a backgroundView property on UITableView and there is also a view for the section header. Not sure if those would help at all. Commented Jul 29, 2012 at 15:42
  • Sadly not, the background of the table is static and sits behind the table. While the header merely sits above the cells in a specific section. I need a background to go behind a section of my table and scroll with it. Commented Jul 29, 2012 at 15:44

3 Answers 3

7

You can place a view behind your table view, set the table view's background colour to [UIColor clearColor] and you will see the view behind

[self.view addSubview:self.sectionBackgroundView]; [self.view addSubview:self.tableView]; self.tableView.backgroundColor = [UIColor clearColor]; 

the drawback to this is that this will not be only limited to the section you want, one way I can think of to make sure this backing view is limited to one section only is to adjust the frame of the backing view to the area covered by the section whenever the table view is scrolled

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGRect sectionFrame = [self.tableView rectForSection:sectionWithBackground]; self.sectionBackgroundView.frame = sectionFrame; } 

you will need to do this at the start (probably in viewWillAppear:) as well to make sure the view starts off correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

quite nice solution but it is not following the section while scrolling in iOS 9...
3

To add background image behind section in UITableView so that it scrolls, write in viewDidAppear

let frame = tableView.rect(forSection: 0) let view = UIView(frame: frame) let imgView = UIImageView(frame: view.bounds) // Assign image here ... view.addSubview(imgView) tableView.addSubview(view) tableView.sendSubviewToBack(view) 

NOTE: Keep the background color of table view cells clear so that background view is visible

Comments

2

Swift 5
write the following code after update data in the table view :

 let frame = tableView.rect(forSection: 0) let backgroundView = UIView(frame: frame) let imageView = UIImageView(frame: view.bounds) imageView.image = your image backgroundView.addSubview(imageView) tableView.addSubview(backgroundView) tableView.sendSubviewToBack(backgroundView) tableView.backgroundColor = .clear 

After that , you will need to bring the cells to the front in willDisplaycell method as the following :

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { tableView.bringSubviewToFront(cell) } 

1 Comment

My UITableView has section headers, so the code should add the section header's height. And if the reloadData() will call several times, don't forget to remove subviews first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.