修改UITableView在UITableViewStyleGrouped形式下的背景色

519 阅读1分钟

为了解决tableview Header 悬浮的问题,一般有两种思路: 一种是在-(void)scrollViewDidScroll:(UIScrollView *)scrollView;方法中来解决; 还有一种方法就是通过将tableView 的样式由UITableViewStylePlain改为UITableViewStyleGrouped;但是改为grouped样式tableview会有默认的背景色,通过设置tableview.backgroundColor 是无法修改背景色的。

通过网上搜索到的方法如下,未能解决。 blog.csdn.net/rbyyyblog/a…

原因是,此时tableview并没有backgroundView的属性。 解决办法如下: 生成一个与tableView尺寸相同的view,将其设置为tableview的backgroundView。

UIView *backgroundView = [[UIView alloc] initWithFrame:_tableView.bounds];
_tableView.backgroundView = backgroundView;
[_tableView.backgroundView setBackgroundColor:UIColor.whiteColor];