首页 > 解决方案 > iOS:为 tableView 和 tableHeaderView 制作不同的背景颜色

问题描述

这是我要存档的内容

我的视图层次结构很简单:我在主视图之上有一个 tableView

        view.backgroundColor = .clear
        tableView.backgroundColor = UIColor.clear
        ...
        cell.backgroundColor = UIColor.clear
        ...
        tableView.tableHeaderView = someView (with clear color)

所以,在设置之后,我有一个背景颜色清晰的表格视图,如果我有一个单元格(我将其背景颜色设置为白色),那么该单元格下方的空间是清晰的。但它应该是白色的。我怎么能得到那个?

- - 解决了:

    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))
        }

标签: iosuitableviewuiviewviewtableview

解决方案


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

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

tableView.tableFooterView = 页脚视图


推荐阅读