【问题标题】:Cant change color of tableHeaderView for UITableView. (Not section header!)无法更改 UITableView 的 tableHeaderView 的颜色。 (不是章节标题!)
【发布时间】:2020-08-12 05:40:47
【问题描述】:

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

所以我像这样添加了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()
    }
}

我试过了,还是不行:

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

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


附:请与tableHeaderView 相关的答案,我知道选项使它像一个节标题或只是一个单元格。谢谢。

【问题讨论】:

    标签: ios swift uitableview uiview


    【解决方案1】:

    使用这个扩展来设置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()
    
    }
    

    【讨论】:

    • 谢谢,您的代码帮助我找到了问题所在。实际上问题是一个超级假人。我忘了设置widthAnchor
    【解决方案2】:

    问题非常简单,只需为我的标题视图设置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)
    ])
    

    附:感谢@jawadAli 帮助发现问题。

    附言约束库TinyConstraints

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 2013-02-18
      • 2011-03-18
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      相关资源
      最近更新 更多