Problem
Changing the UITableView header and footer background color is a common problem. As you know, the general practice is to tableView: viewForHeaderInSection: return a custom view by implementation, with nothing in it, only a background color. But today a more concise approach is found.
A more concise approach
For iOS 6 and later systems, implement this new delegate function:
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { view.tintColor = [UIColor clearColor];}
You can also change the color of text:
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section{ UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view; [footer.textLabel setTextColor:[UIColor whiteColor]];}
The wrong attempt
The purpose of writing this article is mainly to try to record two kinds of mistakes.
When you see this delegate function, the first reaction is to take it for granted:
Error attempt 1
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { view.backgroundColor = [UIColor clearColor];}
This is not valid, no matter what color is not valid.
Error attempt 2
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section{ UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view; footer.contentView.backgroundColor = [UIColor redColor];}
It's no problem to set it up as an opaque color. But set to Clearcolor, see still gray.
Wen/Dai Cang (Jane book author)
Original link: http://www.jianshu.com/p/bfb237f5c20c
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
Change UITableView's headerview, Footerview background color