Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Section Background?

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

I can't think of how to achieve this.

like image 203
Josh Kahane Avatar asked Jul 29 '12 15:07

Josh Kahane


Video Answer


1 Answers

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.

like image 183
wattson12 Avatar answered Sep 22 '22 10:09

wattson12