I have written the following code for adding a text label in the center of the section header view.
Second half of the code is to add a UIButton
whose width is 100 and aligned to the right corner of the section header.
The result is: only the label added shows up at the center. The button does not show up on header at all!
Could you please help pointing out where I'm going wrong in the implementation? Thank you.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
// code for adding centered title
headerView.backgroundColor = UIColor.gray
let headerLabel = UILabel(frame: CGRect(x: 0, y: 0, width:
tableView.bounds.size.width, height: 28))
headerLabel.textColor = UIColor.black
headerLabel.text = titlesList[section]
headerLabel.textAlignment = .center
headerView.addSubview(headerLabel)
// code for adding button to right corner of section header
let showHideButton: UIButton = UIButton(frame: CGRect(x:headerView.frame.size.width - 100, y:0, width:100, height:28))
showHideButton.setTitle("Show Closed", for: .normal)
showHideButton.backgroundColor = UIColor.blue
showHideButton.addTarget(self, action: #selector(btnShowHideTapped), for: .touchUpInside)
headerView.addSubview(showHideButton)
return headerView
}