0

I have an app built with Swift3, with a tableview. I use storyboard, and tableview is placed inside the view controller. I'm using sections and setting section header via code. I want the section header to show the same gray background I have set for the table view. I tried to set the same color code, but it did not help. So I tried setting as UIColor.clear, but it shows as white background for the section header. I need it to use the tableview background. Can you pls guide me on what is going wrong.

Code:

    func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        print("willDisplayFooterView.. for section \(section) ")
        (view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0) 
    }

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        print("willDisplayHeaderView,... for section \(section) ")
        (view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0) 
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        print("titleForHeaderInSection")
        return self.section [section ]
    }

I tried to use viewForHeaderInSection, but it does not get called at all.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        print("in viewForHeaderInSection")
        let view = UIView() 
        let label = UILabel()

        label.text = self.section [section ]            
        view.addSubview(label)
        view.backgroundColor = UIColor.clear

        label.translatesAutoresizingMaskIntoConstraints = false

        let horizontallayoutContraints = NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0)
        view.addConstraint(horizontallayoutContraints)

        let verticalLayoutContraint = NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)
        view.addConstraint(verticalLayoutContraint)

        return view
    }
csharpnewbie
  • 789
  • 2
  • 12
  • 33

2 Answers2

1

Try setting the background color of the view.. instead of the view.backgroundView

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        print("willDisplayFooterView.. for section \(section) ")
        (view as! UITableViewHeaderFooterView).backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0) 
    }

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        print("willDisplayHeaderView,... for section \(section) ")
        (view as! UITableViewHeaderFooterView).backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0) 
    }

DISCLAIMER: I am not very good at swift syntax.. but hope this answer your question.

  • hi.. still it does not give me the expected behavior. I tried using as you suggested and got warning that it is a deprecated behavior and to use contentView background color instead. So I tried using that, but still no luck. func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { print("willDisplayFooterView.. for section \(section) ") (view as! UITableViewHeaderFooterView).contentView.backgroundColor = UIColor.clear } – csharpnewbie Aug 02 '17 at 21:14
  • are you overriding this method? "func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?" – Marcio Romero Patrnogic Aug 02 '17 at 21:23
  • hi.. as I had edited in the original question, I tried to use viewForHeaderInSection, but it does not get called at all. – csharpnewbie Aug 02 '17 at 21:39
  • try overriding the height for header in section method – Marcio Romero Patrnogic Aug 02 '17 at 23:12
  • Yes, I do have a override for heightForHeaderInSection and thats where I set the height for my section header. Am not sure how to set background color to clear in that. – csharpnewbie Aug 02 '17 at 23:59
  • I figured out the issue as to why viewForHeaderInSection is not getting called. It is part of UITableViewDelegate and I had it in the wrong place, hence I faced the issue. Now it gets called, and am able to set my view correctly. Thanks for your help. – csharpnewbie Aug 03 '17 at 00:14
1

You can change the background color of contentView instead of backgroundView

(view as! UITableViewHeaderFooterView).contentView?.backgroundColor = UIColor. groupTableViewBackground
cyberme
  • 21
  • 3
  • yes, I tried that too, as per Marcio's suggestion, but it did not work. – csharpnewbie Aug 02 '17 at 21:40
  • Marcio suggested changing backgroundColor of view not the contentView. Have you tried changing backgroundColor of the view's contentView ? – cyberme Aug 02 '17 at 21:45
  • yes, I tried using as he suggested and got warning that it is a deprecated behavior and to use contentView background color instead. So I tried using that, but still no luck. func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { print("willDisplayFooterView.. for section (section) ") (view as! UITableViewHeaderFooterView).contentView.backgroundColor = UIColor.clear } – csharpnewbie Aug 02 '17 at 22:36