Ios 设置UITableHeaderFooterView背景色时出错
我的TableView有一个自定义的Header类,每当使用它时,我总是会遇到以下错误: [TableView]不推荐在UITableViewHeaderFooterView上设置背景色。请改为将具有所需背景颜色的自定义UIView设置为backgroundView属性 以下是我尝试过的一些事情: 我在UITableViewHeaderFooterView子类的awakeFromNib()中添加了一个backgroundView:Ios 设置UITableHeaderFooterView背景色时出错,ios,swift,uitableview,uiview,Ios,Swift,Uitableview,Uiview,我的TableView有一个自定义的Header类,每当使用它时,我总是会遇到以下错误: [TableView]不推荐在UITableViewHeaderFooterView上设置背景色。请改为将具有所需背景颜色的自定义UIView设置为backgroundView属性 以下是我尝试过的一些事情: 我在UITableViewHeaderFooterView子类的awakeFromNib()中添加了一个backgroundView: override func awakeFromNib() {
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundView = UIView(frame: self.bounds)
self.backgroundView?.backgroundColor = UIColor(red:0.84, green:0.47, blue:0.97, alpha:1.0)
}
我在tableView函数中添加了一个backgroundView,用于定义标题部分:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: Header.identifier) as? Header {
// Extraneous Code
let backgroundView = UIView(frame: headerView.bounds)
backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
headerView.backgroundView = backgroundView
// Extraneous code
}
return UIView()
}
在做了每一步之后,我仍然会得到错误。检查您必须设置
headerView.backgroundColor=UIColor(白色:0.5,阿尔法:0.5)
在func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView?
函数中
此外,这将足以添加背景色
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let backgroundView = UIView(frame: CGRect.zero)
backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
return backgroundView
}
检查您必须设置的
headerView.backgroundColor=UIColor(白色:0.5,alpha:0.5)
在func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView?
函数中
此外,这将足以添加背景色
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let backgroundView = UIView(frame: CGRect.zero)
backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
return backgroundView
}
如果可以将UITableViewHeaderFooterView子类化,则可以尝试这种方法
class SampleHeaderView: UITableViewHeaderFooterView {
override func awakeFromNib() {
super.awakeFromNib()
let bgView = UIView(color: .black)
addSubview(bgView)
bgView.translatesAutoresizingMaskIntoConstraints = false
let layoutAttributes: [NSLayoutAttribute] = [.top, .leading, .bottom, .trailing]
layoutAttributes.forEach { attribute in
addConstraint(NSLayoutConstraint(item: bgView,
attribute: attribute,
relatedBy: .equal,
toItem: self,
attribute: attribute,
multiplier: 1,
constant: 0.0))
}
}
}
extension UIView {
convenience init(color: UIColor) {
self.init(frame: .zero)
backgroundColor = color
}
}
如果可以将UITableViewHeaderFooterView子类化,则可以尝试这种方法
class SampleHeaderView: UITableViewHeaderFooterView {
override func awakeFromNib() {
super.awakeFromNib()
let bgView = UIView(color: .black)
addSubview(bgView)
bgView.translatesAutoresizingMaskIntoConstraints = false
let layoutAttributes: [NSLayoutAttribute] = [.top, .leading, .bottom, .trailing]
layoutAttributes.forEach { attribute in
addConstraint(NSLayoutConstraint(item: bgView,
attribute: attribute,
relatedBy: .equal,
toItem: self,
attribute: attribute,
multiplier: 1,
constant: 0.0))
}
}
}
extension UIView {
convenience init(color: UIColor) {
self.init(frame: .zero)
backgroundColor = color
}
}
在使用名称
backgroundView
时,您是否会收到以下错误:
1) “backgroundView”使用不明确
2) 无法重写可变属性“backgroundView”
3) 无法使用存储属性“backgroundView”重写
由于您没有添加背景视图
,因此您使用的是UITableViewHeaderFooterView
所具有的背景视图
对于awakeFromNib
:
override func awakeFromNib() {
super.awakeFromNib()
let bgView = UIView(frame: self.bounds)
bgView.backgroundColor = UIColor(red:0.84, green:0.47, blue:0.97, alpha:1.0)
self.addSubview(bgView)
}
对于标题部分的视图,请参见:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: Header.identifier) as? Header {
// Extraneous Code
let bgView = UIView(frame: headerView.bounds)
bgView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
headerView.addSubview(bgView)
return headerView
// Extraneous code
}
return UIView()
}
您只需将名称
backgroundView
更改为其他名称。使用名称backgroundView
时,是否会出现以下错误:
1) “backgroundView”使用不明确
2) 无法重写可变属性“backgroundView”
3) 无法使用存储属性“backgroundView”重写
由于您没有添加背景视图
,因此您使用的是UITableViewHeaderFooterView
所具有的背景视图
对于awakeFromNib
:
override func awakeFromNib() {
super.awakeFromNib()
let bgView = UIView(frame: self.bounds)
bgView.backgroundColor = UIColor(red:0.84, green:0.47, blue:0.97, alpha:1.0)
self.addSubview(bgView)
}
对于标题部分的视图,请参见:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: Header.identifier) as? Header {
// Extraneous Code
let bgView = UIView(frame: headerView.bounds)
bgView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
headerView.addSubview(bgView)
return headerView
// Extraneous code
}
return UIView()
}
您只需将名称
backgroundView
更改为其他名称。我能够防止出现错误。我是这样做的:
转到标题的Xib文件,选择默认背景色作为背景色。然后,在viewForHeaderInSection函数中,添加:
let backgroundView = UIView(frame: CGRect.zero)
backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
headerView.backgroundView = backgroundView
我知道我在问题中提到过,但关键是将xib背景色设置为“默认值”。我能够防止出现错误。我是这样做的: 转到标题的Xib文件,选择默认背景色作为背景色。然后,在viewForHeaderInSection函数中,添加:
let backgroundView = UIView(frame: CGRect.zero)
backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
headerView.backgroundView = backgroundView
我知道我在问题中提到过,但关键是将xib背景色设置为“默认值”。您正在尝试返回一个新的干净UIView()。你的观点,你在上面设置的,没有意义。改为在viewForHeader中返回headerView。您正在尝试返回新的干净UIView()。你的观点,你在上面设置的,没有意义。在viewForHeader中返回您的headerView。