I would like to change the font type and font size of a section header in a table view controller.
My code:
func tableView(tableView: UITableView, willDi
Updated for Xcode 11, Swift 5
The top voted answers didn't work for me, here's what did:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
/// Create the view.
let headerView = UIView()
headerView.backgroundColor = .clear
/// Create the label that goes inside the view.
let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width: tableView.bounds.size.width, height: 30))
headerLabel.font = UIFont(name: "myFont", size: 12)
headerLabel.textColor = .white
headerLabel.text = "myHeaderName"
headerLabel.sizeToFit()
/// Add label to the view.
headerView.addSubview(headerLabel)
/// Return view.
return headerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
/// Return the same height as the height of the label in your header view.
return 30
}