[英]Cant change color of tableHeaderView for UITableView. (Not section header!)
I need a header for my UITableView
.我的 UITableView 需要一个
UITableView
。 The section header is not working for me 'cause it shouldn't scroll with cells. header 部分对我不起作用,因为它不应该与单元格一起滚动。
So I added tableView.tableHeaderView
like this:所以我像这样添加了
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()
}
}
I tried, and it's not working:我试过了,它不起作用:
Adding a new container view for the label, edge it to superview, and change color for this container view.为 label 添加新的容器视图,将其边缘添加到超级视图,并更改此容器视图的颜色。 It looks like this
UIView
inherits color from UITableView
.看起来这个
UIView
从UITableView
继承颜色。
Question: Maybe someone met some problems and know how to change color of tableHeaderView
?问题:也许有人遇到了一些问题并且知道如何改变
tableHeaderView
的颜色?
PS Please, answers related to tableHeaderView
, I know about option make it like a section header or just a cell. PS 请,与
tableHeaderView
相关的答案,我知道选项使它像一个部分 header 或只是一个单元格。 Thanks.谢谢。
Use this extension to set headerView使用这个扩展来设置 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
}
}
then in you controller然后在你 controller
override func viewDidLoad() {
super.viewDidLoad()
let hView = UIView()
self.tableView.setTableHeaderView(headerView: hView)
NSLayoutConstraint.activate([
hView.heightAnchor.constraint(equalToConstant: 100),
])
self.tableView.updateHeaderViewFrame()
}
The problem was super simple, just to set the width
constraint for my header view.问题非常简单,只需为我的 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 Thanks to @jawadAli for helping found a problem. PS 感谢@jawadAli 帮助发现问题。
PSS Library for constraints TinyConstraints
PSS 约束库
TinyConstraints
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.