改变UITableView的headerView、footerView背景颜色

问题

改变UITableView的header、footer背景颜色,这是个很常见的问题。以前知道的通常作法是,经过实现tableView: viewForHeaderInSection:返回一个自定义的View,里面什么都不填,只设背景颜色。可是今天发现一个更简洁的作法。函数

更简洁的方法

对于iOS 6及之后的系统,实现这个新的delegate函数便可:ui

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { view.tintColor = [UIColor clearColor]; }

还能够改变文字的颜色:spa

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view; [footer.textLabel setTextColor:[UIColor whiteColor]]; }

错误的尝试

写这篇文章的目的,主要是想记录两种错误的尝试。
当看到这个Delegate函数时,第一反应是想固然地这样作:code

错误尝试1

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { view.backgroundColor = [UIColor clearColor]; }

这样作是无效的,不管对什么颜色都无效。it

错误尝试2

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view; footer.contentView.backgroundColor = [UIColor redColor]; }

这样作设成不透明的颜色就没问题。但设成clearColor,看到的仍是灰色。io



文/戴仓薯(简书做者) 原文连接:http://www.jianshu.com/p/bfb237f5c20c 著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。
相关文章
相关标签/搜索