UITableView - change section header color

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

    I think this code is not so bad.

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(MyHeaderView.reuseIdentifier) as MyHeaderView
        let backgroundView = UIView()
        backgroundView.backgroundColor = UIColor.whiteColor()
        headerView.backgroundView = backgroundView
        headerView.textLabel.text = "hello"
        return headerView
    }
    
    0 讨论(0)
  • 2020-11-27 09:15

    Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.

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

    In my case, It worked like this:

    let headerIdentifier = "HeaderIdentifier"
    let header = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: headerIdentifier)
    header.contentView.backgroundColor = UIColor.white
    
    0 讨论(0)
  • 2020-11-27 09:16

    For swift 5 +

    In willDisplayHeaderView Method

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
         //For Header Background Color
         view.tintColor = .black
    
        // For Header Text Color
        let header = view as! UITableViewHeaderFooterView
        header.textLabel?.textColor = .white
    }
    

    I hope this helps you :]

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

    In iOS 7.0.4 I created a custom header with it's own XIB. Nothing mentioned here before worked. It had to be the subclass of the UITableViewHeaderFooterView to work with the dequeueReusableHeaderFooterViewWithIdentifier: and it seems that class is very stubborn regarding the background color. So finally I added an UIView (you could do it either with code or IB) with name customBackgroudView, and then set it's backgroundColor property. In layoutSubviews: I set that view's frame to bounds. It work with iOS 7 and gives no glitches.

    // in MyTableHeaderView.xib drop an UIView at top of the first child of the owner
    // first child becomes contentView
    
    // in MyTableHeaderView.h
    @property (nonatomic, weak) IBOutlet UIView * customBackgroundView;
    
    // in MyTableHeaderView.m
    -(void)layoutSubviews;
    {
        [super layoutSubviews];
    
        self.customBackgroundView.frame = self.bounds;
    }
    // if you don't have XIB / use IB, put in the initializer:
    -(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
    {
        ...
        UIView * customBackgroundView = [[UIView alloc] init];
        [self.contentView addSubview:customBackgroundView];
        _customBackgroundView = customBackgroundView;
        ...
    }
    
    
    // in MyTableViewController.m
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        MyTableHeaderView * header = [self.tableView
                                              dequeueReusableHeaderFooterViewWithIdentifier:@"MyTableHeaderView"];
        header.customBackgroundView.backgroundColor = [UIColor redColor];
        return header;
    }
    
    0 讨论(0)
  • 2020-11-27 09:19

    Based on @Dj S answer, using Swift 3. This works great on iOS 10.

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        // Background color
        view.tintColor = UIColor.black
    
        // Text Color
        let headerView = view as! UITableViewHeaderFooterView
        headerView.textLabel?.textColor = UIColor.white
    }
    
    0 讨论(0)
提交回复
热议问题