我有一个使用Swift3构建的应用程序,具有表格视图。我使用情节提要,并将tableview放置在视图控制器中。我正在使用节并通过代码设置节标题。我希望节标题显示与表格视图相同的灰色背景。我尝试设置相同的颜色代码,但没有帮助。因此,我尝试将其设置为UIColor.clear,但该节标题显示为白色背景。我需要它来使用tableview背景。您能否指导我解决问题。
码:
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 ]
}
我尝试使用viewForHeaderInSection,但根本没有调用它。
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
}
最佳答案
尝试设置视图的背景颜色。而不是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)
}
免责声明:我不太擅长快速语法..但是希望这能回答您的问题。
关于ios - 在swift3中的节标题中使用时,UIColor.Clear没有给出预期的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45470861/