Well, I am trying to make the headers of UITableView compatible with iPhone X Landscape where I want background colors of Header should expand to edge. I have seen the WWDC video to do that where they said to apply color on backgroundView but in my case it's always nil.
Here is my code and also link to demo of it.
extension ViewController:UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String.init(describing: HVTableViewCell.self)) as! HVTableViewCell
return cell
}
}
extension ViewController:UITableViewDelegate{
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60.0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: String.init(describing: HVTableViewHeaderFooterView.self)) as? HVTableViewHeaderFooterView{
if let backgroundView = headerView.backgroundView{
backgroundView.backgroundColor = UIColor.red
}else{
print("Failed")
}
return headerView
}else{
return nil
}
}
}
class ViewController: UIViewController {
@IBOutlet weak var tblReference: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tblReference.register(UINib.init(nibName: String.init(describing: HVTableViewCell.self), bundle: nil), forCellReuseIdentifier: String.init(describing: HVTableViewCell.self))
self.tblReference.register(UINib.init(nibName: String.init(describing: HVTableViewHeaderFooterView.self), bundle: nil), forHeaderFooterViewReuseIdentifier: String.init(describing: HVTableViewHeaderFooterView.self))
self.tblReference.delegate = self
self.tblReference.dataSource = self
self.tblReference.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the screenshot of the issue.
Thanks in advance.