UITableView - change section header color

前端 未结 30 2641
不思量自难忘°
不思量自难忘° 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:28

    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)section
    

    section 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 3 / 4

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
        view.tintColor = UIColor.red
        let header = view as! UITableViewHeaderFooterView
        header.textLabel?.textColor = UIColor.white
    }
    
    0 讨论(0)
  • 2020-11-27 09:28

    If you don't want to create a custom view, you can also change the color like this (requires iOS 6):

    -(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;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 09:29

    Set the background and text color of section area: (Thanks to William Jockusch and Dj S)

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
    {
        if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
            UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
            castView.contentView.backgroundColor = [UIColor grayColor];
            [castView.textLabel setTextColor:[UIColor grayColor]];
        }
    }
    
    0 讨论(0)
  • 2020-11-27 09:29
     -(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 whiteColor]; // substitute your color here
                 content.backgroundColor = color;
                 [castView.textLabel setTextColor:[UIColor blackColor]];
            }
     }
    
    0 讨论(0)
  • 2020-11-27 09:31

    Hopefully this method from the UITableViewDelegate protocol will get you started:

    Objective-C:

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    {
      UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
      if (section == integerRepresentingYourSectionOfInterest)
         [headerView setBackgroundColor:[UIColor redColor]];
      else 
         [headerView setBackgroundColor:[UIColor clearColor]];
      return headerView;
    }
    

    Swift:

    func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
    {
      let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
      if (section == integerRepresentingYourSectionOfInterest) {
        headerView.backgroundColor = UIColor.redColor()
      } else {
        headerView.backgroundColor = UIColor.clearColor()
      }
      return headerView
    }
    

    Updated 2017:

    Swift 3:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
        {
            let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
            if (section == integerRepresentingYourSectionOfInterest) {
                headerView.backgroundColor = UIColor.red
            } else {
                headerView.backgroundColor = UIColor.clear
            }
            return headerView
        }
    

    Replace [UIColor redColor] with whichever UIColor you would like. You may also wish to adjust the dimensions of headerView.

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

    Don't forget to add this piece of code from the delegate or your view will be cut off or appear behind the table in some cases, relative to the height of your view/label.

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 30;
    }
    
    0 讨论(0)
提交回复
热议问题