I'm trying to customize header in section in UITableView. I couldn't find to do it in storyboard so I started adding UIView and UILabel.
Here is my code:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0,0, tableView.frame.size.width, 60))
headerView.backgroundColor = UIColor.cyanColor()
headerView.layer.borderColor = UIColor.whiteColor().CGColor
headerView.layer.borderWidth = 1.0;
let headerLabel = UILabel(frame: CGRectMake(5,2, tableView.frame.size.width - 5, 30))
headerLabel.text = sectionTitle (this is a variable)
headerLabel.textAlignment = NSTextAlignment.Center
headerLabel.font = UIFont(name: "AvenirNext", size: 18)
headerLabel.textAlignment = NSTextAlignment.Center;
headerView.addSubview(headerLabel)
return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
I am looking for a way to locate UILabel vertically and horizontally center of the view. How would I do that? Would it always change whenever I change the font and size?
Also, would there by anyway I can do this in storyboard? It is really hard to make header programmatically.