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
}