Reputation: 595
I use xib to custom headerView and set default background color to this headerView, and I always get this warning. How to fix it? Thanks.
these property are not work for me.
Upvotes: 0
Views: 3435
Reputation: 1078
This is working fine...
Upvotes: 0
Reputation: 1710
I use this delegate method, with success ~! cut-n-paste coder, here we go ~!
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
id maybe = view;
if ([maybe isKindOfClass:UITableViewHeaderFooterView.class]) {
UITableViewHeaderFooterView *header = maybe;
header.contentView.backgroundColor = UIColor.blueColor;
}
}
Upvotes: 0
Reputation: 955
You have to set the color of contentView
e.g.
class HeaderView: UITableViewHeaderFooterView {
@IBOutlet weak var txtLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
self.txtLabel.textColor = .white
self.contentView.backgroundColor = .red
}
func bind(text: String) {
self.txtLabel.text = text
}
}
Upvotes: 0
Reputation: 472
I found that setting the background color property in IB to default solves the issue with the warning. However my top level item is view and yours is cell, which may have different default color, that causes the warning.
Upvotes: 1
Reputation: 20379
Not really sure, what did you mean by I already tried, clearly from the code snippet you posted you havent, You can either do this tableView delegate or in awakeFromNib
of your headerView itself
guard let header =
detailTableView.dequeueReusableHeaderFooterView(withIdentifier: "DetailListHeaderView") as? DetailListHeaderView else { fatalError("could not create headerView") }
let backgroundView = UIView(frame: header.bounds)
backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
header.backgroundView = backgroundView
//other codes of yours
return header
Upvotes: 1