UITableView - change section header color

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

    Swift 4

    To change the background color, text label color and font for the Header View of a UITableView Section, simply override willDisplayHeaderView for your table view like so:

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            let header = view as! UITableViewHeaderFooterView
            header.backgroundView?.backgroundColor = .white
            header.textLabel?.textColor = .black
            header.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
    } 
    

    This worked perfectly for me; hope it does help you too!

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

    Here's how to add an image in header view:

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    {
        UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
        UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];
    
        headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);
    
        [headerView addSubview:headerImage];
    
        return headerView;
    }
    
    0 讨论(0)
  • For iOS8 (Beta) and Swift choose the RGB Color you want and try this:

    override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
        var header :UITableViewHeaderFooterView = UITableViewHeaderFooterView()
    
        header.contentView.backgroundColor = UIColor(red: 254.0/255.0, green: 190.0/255.0, blue: 127.0/255.0, alpha: 1)
        return header
    

    }

    (The "override" is there since i´m using the UITableViewController instead of a normal UIViewController in my project, but it´s not mandatory for changing the section header color)

    The text of your header will still be seen. Note that you will need to adjust the section header height.

    Good Luck.

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

    I know its answered, just in case, In Swift use the following

        override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let tableViewWidth = self.tableView.bounds
    
            let headerView = UIView(frame: CGRectMake(0, 0, tableViewWidth.size.width, self.tableView.sectionHeaderHeight))
            headerView.backgroundColor = UIColor.greenColor()
    
            return headerView
        }
    
    0 讨论(0)
  • 2020-11-27 09:32

    Just change the color of layer of the header view

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    {
      UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,    tableView.bounds.size.width, 30)] autorelease];
     headerView.layer.backgroundColor = [UIColor clearColor].CGColor
    }
    
    
    0 讨论(0)
  • 2020-11-27 09:32

    If anyone needs swift, keeps title:

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0,y: 0,width: self.tableView.frame.width, height: 30))
        view.backgroundColor = UIColor.redColor()
        let label = UILabel(frame: CGRect(x: 15,y: 5,width: 200,height: 25))
        label.text = self.tableView(tableView, titleForHeaderInSection: section)
        view.addSubview(label)
        return view
    }
    
    0 讨论(0)
提交回复
热议问题