Date
Sep. 8th, 2024
 
2024年 8月 6日

Post: iOS UITableView: Appearance of HeaderView & FooterView for Section

iOS UITableView: Appearance of HeaderView & FooterView for Section

Published 12:09 Sep 16, 2015.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

如果你想自定义header颜色,可以这样做:

[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];

如果你不想建立自定义视图,也可以这样(iOS6):

-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        UIView* content = castView.contentView;
        UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
        content.backgroundColor = color;
    }
}

(通过UITableViewHeaderFooterView设置背景色的方法已经被废弃了。请用contentView.backgroundColor代替。)

在iOS 6以上,你可以通过以下方法改变背景色和文本色:

- (void)tableView:(UITableView *)tableView
        willDisplayHeaderView:(UIView *)view
        forSection:(NSInteger)section;

例如:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor blackColor];

    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];

    // Another way to Setter the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}

Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系