无法更改 UITableView 的 tableHeaderView 的颜色。 (不是第 header 节!)

Cant change color of tableHeaderView for UITableView. (Not section header!)

我的 UITableView 需要一个 header。 header 部分对我不起作用,因为它不应该随单元格一起滚动。

所以我添加了 tableView.tableHeaderView 这样的:

 // ViewController

 tableView.tableFooterView = UIView()
 let header = SearchPopularHeaderView()
 header.height(36)
 tableView.tableHeaderView = header

 // ...
 // SearchPopularHeaderView

class SearchPopularHeaderView: UIView {

    private var lblTitle: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)

        viewDidLoad()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)

        viewDidLoad()
    }

    private func viewDidLoad() {
        lblTitle = UILabel()
        lblTitle.text = "Most popular".localized()
        lblTitle.font = UIFont.systemFont(ofSize: 12, weight: .bold)
        lblTitle.textColor = UIColor("#e6f2f9")
        self.addSubview(lblTitle)

        lblTitle.centerY(to: self)
        lblTitle.leadingToSuperview(offset: 16)

        self.backgroundColor = UIColor("#3c4958")

        layoutIfNeeded()
    }
}

我试过了,还是不行:

为标签添加一个新的容器视图,将其边缘化为超级视图,并更改此容器视图的颜色。看起来 UIView 继承了 UITableView 的颜色。

问题:也许有人遇到了一些问题,知道如何改变tableHeaderView的颜色?


P.S。拜托,与 tableHeaderView 相关的答案,我知道选项使它像一个部分 header 或只是一个单元格。谢谢。

使用此扩展设置 headerView

extension UITableView {

    /// Set table header view & add Auto layout.
    func setTableHeaderView(headerView: UIView) {
        headerView.translatesAutoresizingMaskIntoConstraints = false

        // Set first.
        self.tableHeaderView = headerView

        // Then setup AutoLayout.
        headerView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        headerView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
        headerView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    }

    /// Update header view's frame.
    func updateHeaderViewFrame() {
        guard let headerView = self.tableHeaderView else { return }

        // Update the size of the header based on its internal content.
        headerView.layoutIfNeeded()

        // ***Trigger table view to know that header should be updated.
        let header = self.tableHeaderView
        self.tableHeaderView = header
    }
}

然后在你的控制器中

override func viewDidLoad() {
        super.viewDidLoad()

     let hView = UIView()
     self.tableView.setTableHeaderView(headerView: hView)

    NSLayoutConstraint.activate([
        hView.heightAnchor.constraint(equalToConstant: 100),
    ])
    self.tableView.updateHeaderViewFrame()

}

问题非常简单,只需为我的 header 视图设置 width 约束即可。


// With 'TinyConstraints'

let hView = SearchPopularHeaderView()
self.tableView.tableHeaderView = hView
sectionHeaderHeight = hView.height(36)
hView.width(to: sview.tableView)

// Without 'TinyConstraints'

let hView = SearchPopularHeaderView()
sview.tableView.tableHeaderView = hView
NSLayoutConstraint.activate([
  hView.heightAnchor.constraint(equalToConstant: 36),
  hView.widthAnchor.constraint(equalTo: self.tableView.widthAnchor)
])

P.S。感谢@jawadAli 帮忙发现问题

P.S.S.约束库 TinyConstraints