UITableView change header title color

后端 未结 6 634
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 10:18

I\'m styling the UITableView in InAppSettingsKit and want to change the color of the header title:

\"image

6条回答
  •  一向
    一向 (楼主)
    2021-02-13 10:51

    This is an old question, but I think the answer needs to be updated.

    This method does not involve defining and creating your own custom view. In iOS 6 and up, you can easily change the background color and the text color by defining the

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

    delegate method.

    For example:

    - (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 set the background color
        // Note: does not preserve gradient effect of original header
        // header.contentView.backgroundColor = [UIColor blackColor];
    }
    

    Taken from my post here: https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

    Swift 5.0:

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let header = view as? UITableViewHeaderFooterView {
            header.textLabel?.textColor = .white
        }
    }
    

提交回复
热议问题