iOS:为tableView和tableHeaderView制作不同的背景色

iOS:为tableView和tableHeaderView制作不同的背景色,ios,uitableview,uiview,view,tableview,Ios,Uitableview,Uiview,View,Tableview,以下是我试图归档的内容 我的主视图是带有渐变的红色 tableHeaderView应该有一个清晰的颜色(因为它是一个具有圆角的视图,所以在某些部分显示圆角时应该是清晰的) tableView(标题除外)应为白色。即使只有一个单元格,该单元格下方的底部空间也应为白色 我的视图层次结构很简单:我在主视图的顶部有一个tableView view.backgroundColor = .clear tableView.backgroundColor = UIColor.

以下是我试图归档的内容

  • 我的主视图是带有渐变的红色
  • tableHeaderView应该有一个清晰的颜色(因为它是一个具有圆角的视图,所以在某些部分显示圆角时应该是清晰的)
  • tableView(标题除外)应为白色。即使只有一个单元格,该单元格下方的底部空间也应为白色
我的视图层次结构很简单:我在主视图的顶部有一个tableView

        view.backgroundColor = .clear
        tableView.backgroundColor = UIColor.clear
        ...
        cell.backgroundColor = UIColor.clear
        ...
        tableView.tableHeaderView = someView (with clear color)
所以,在设置之后,我有了背景色清晰的tableview,如果我有一个单元格(我将其背景色设置为白色),那么该单元格下面的空间是清晰的。但它应该是白色的。我怎么能得到这个

---已解决:

    tableView.tableFooterView = UIView()
        if let tableFooterView = tableView.tableFooterView {
            let bigFooterView = UIView()
            bigFooterView.backgroundColor = .white
            tableFooterView.addSubview(bigFooterView)

            bigFooterView.translatesAutoresizingMaskIntoConstraints = false
            tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .trailing, relatedBy: .equal, toItem: tableFooterView, attribute: .trailing, multiplier: 1, constant: 0))
            tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .leading, relatedBy: .equal, toItem: tableFooterView, attribute: .leading, multiplier: 1, constant: 0))
            tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .top, relatedBy: .equal, toItem: tableFooterView, attribute: .top, multiplier: 1, constant: 0))
            tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: UIScreen.main.bounds.height*2))
        }

将表尾设置为白色视图

让footerView=UIView(帧:CGRect(x:0,y:0,宽度:0,高度:someHeight)footerView.backgroundColor=.white

tableView.tableFooterView=footerView


对不起,忘记设置高度了。