UITableView - change section header color

前端 未结 30 2642
不思量自难忘°
不思量自难忘° 2020-11-27 08:50

How can I change color of a section header in UITableView?

EDIT: The answer provided by DJ-S should be considered for iOS 6 and above. The accepted

相关标签:
30条回答
  • 2020-11-27 09:19

    With RubyMotion / RedPotion, paste this into your TableScreen:

      def tableView(_, willDisplayHeaderView: view, forSection: section)
        view.textLabel.textColor = rmq.color.your_text_color
        view.contentView.backgroundColor = rmq.color.your_background_color
      end
    

    Works like a charm!

    0 讨论(0)
  • 2020-11-27 09:20

    You can do this if you want header with custom color:

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

    This solution works great since iOS 6.0.

    0 讨论(0)
  • 2020-11-27 09:21

    Here's how to change the text color.

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
    label.text = @"Section Header Text Here";
    label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
    label.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];
    
    0 讨论(0)
  • 2020-11-27 09:22

    iOS 8+

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            tableView.tableHeaderView?.backgroundColor = UIColor.blue()
    }
    
    0 讨论(0)
  • 2020-11-27 09:23

    I have a project using static table view cells, in iOS 7.x. willDisplayHeaderView does not fire. However, this method works ok:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        NSLog(@"%s", __FUNCTION__);
        CGRect headerFrame = CGRectMake(x, y, w, h);    
        UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];  
        headerView.backgroundColor = [UIColor blackColor];
    
    0 讨论(0)
  • 2020-11-27 09:23

    Swift 4 makes it very easy. Simply add this to your class and set the color as needed.

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            view.backgroundColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
        }
    

    or if a simple color

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            view.backgroundColor = UIColor.white
        }
    

    Updated for Swift 5

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            view.tintColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
        }
    

    or if a simple color

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            view.tintColor = UIColor.white
        }
    
    0 讨论(0)
提交回复
热议问题