テーブルビューを備えたSwift3で構築されたアプリがあります。ストーリーボードを使用し、テーブルビューはビュー コントローラー内に配置します。セクションを使用し、コードでセクション ヘッダーを設定しています。テーブル ビューに設定したのと同じ灰色の背景をセクション ヘッダーに表示したいと考えています。同じカラーコードを設定しようとしましたが、うまくいきませんでした。ということでUIColor.clearに設定してみたのですが、セクションヘッダーの背景が白く表示されてしまいます。テーブルビューの背景を使用するために必要です。何がうまくいかないのか教えてください。
コード:
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
}