Change UITableView's headerview, Footerview background color

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.