Here's a weird one -- I finally got an Apple Silicon MacBook Pro and I'm trying to fix a few display issues when running my iOS/iPadOS app on the Apple Silicon. For some reason, the tableview, specifically, willDisplayHeaderView view returns as Nil.
This example code crashes due to required:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = view as! UITableViewHeaderFooterView
headerView.textLabel!.textColor = UIColor.white
let font = UIFont(name: "Avenir-Medium", size: 16.0)
headerView.textLabel!.font = font
headerView.backgroundView!.backgroundColor = UIColor.init(red: 28, green: 28, blue: 30, transparancy: 100)
headerView.tintColor = UIColor.init(red: 28, green: 28, blue: 30, transparancy: 100)
headerView.textLabel!.textAlignment = .center
}
This example code, no crash because they are optional but also no effect:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = view as! UITableViewHeaderFooterView
headerView.textLabel?.textColor = UIColor.white
let font = UIFont(name: "Avenir-Medium", size: 16.0)
headerView.textLabel?.font = font
headerView.backgroundView?.backgroundColor = UIColor.init(red: 28, green: 28, blue: 30, transparancy: 100)
headerView.tintColor = UIColor.init(red: 28, green: 28, blue: 30, transparancy: 100)
headerView.textLabel?.textAlignment = .center
}
Any ideas why the running via XCODE "My Mac (Designed for iPad)" causes the header/footer view to come back as Nil? Works fine on real iPad and iOS compile/simulator.
CodePudding user response:
Looks like "textLabel" is deprecated, this is how you need to do it for iOS 14 :
let headerView = view as! UITableViewHeaderFooterView
if #available(iOS 14.0, *) {
var config = headerView.defaultContentConfiguration()
config.text = "Hello"
headerView.contentConfiguration = config
} else {
// Fallback on earlier versions
headerView.textLabel?.text = "Hello"
}
