Search code examples
iosswiftheadertableviewtitle

How to separate Header view background color with the table view in swift


so I want to make something similar like this from Health App, where the 'Favorites' title is separated with the cell.

enter image description here

Here's what my UI now :

enter image description here

So I want to make the background curves on the first cell, instead of the header title. Is that possible?

I've been looking around and I didn't get any solution. Here's the code I have created so far :

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = UILabel()
    label.text = "Smaller Title"
    label.font = .boldSystemFont(ofSize: 22)
    label.textColor = UIColor.black
    label.backgroundColor = UIColor.clear
    label.textAlignment = .left
    label.translatesAutoresizingMaskIntoConstraints = false
    
    let headerView = UIView()
    headerView.backgroundColor = UIColor.clear
    headerView.addSubview(label)
    label.leftAnchor.constraint(equalTo: headerView.leftAnchor).isActive = true

    return headerView
}

Solution

  • Here the UITableView has a .white backgroundColor. Try setting the backgroundColor of UITableView as .clear and customize the UITableViewCells to have the .white backgroundColor.

    tableView.backgroundColor = .clear