改变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
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { view.backgroundColor = [UIColor clearColor]; }
这样作是无效的,不管对什么颜色都无效。it
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view; footer.contentView.backgroundColor = [UIColor redColor]; }
这样作设成不透明的颜色就没问题。但设成clearColor,看到的仍是灰色。io