首页 > 解决方案 > 如何在swift中将标题视图背景颜色与表格视图分开

问题描述

所以我想从 Health App 中制作类似的东西,其中“收藏夹”标题与单元格分开。

在此处输入图像描述

这是我现在的用户界面:

在此处输入图像描述

所以我想在第一个单元格上制作背景曲线,而不是标题。那可能吗?

我一直在环顾四周,但没有得到任何解决方案。这是我到目前为止创建的代码:

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
}

标签: iosswiftheadertableviewtitle

解决方案


这里UITableView有一个.white backgroundColor. 尝试将 设置backgroundColorUITableView.clear自定义UITableViewCells以拥有.white backgroundColor.

tableView.backgroundColor = .clear

推荐阅读