簡體   English   中英

無法更改 UITableView 的 tableHeaderView 的顏色。 (不是節標題!)

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

我的 UITableView 需要一個UITableView 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()
    }
}

我試過了,它不起作用:

為 label 添加新的容器視圖,將其邊緣添加到超級視圖,並更改此容器視圖的顏色。 看起來這個UIViewUITableView繼承顏色。

問題:也許有人遇到了一些問題並且知道如何改變tableHeaderView的顏色?


PS 請,與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
    }
}

然后在你 controller

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

PS 感謝@jawadAli 幫助發現問題。

PSS 約束庫TinyConstraints

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM