iOS:为tableView和tableHeaderView制作不同的backgroundColor

时间:2018-05-19 00:57:39

标签: ios uitableview uiview view tableview

以下是我要归档的内容

  • 我的主视图有红色渐变色。
  • 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))
        }

1 个答案:

答案 0 :(得分:0)

将表格页脚设置为白色视图。

  

让footerView = UIView(frame:CGRect(x:0,y:0,width:0,height:someHeight)footerView.backgroundColor = .white

     

tableView.tableFooterView = footerView