Beginnerrrrrr
Beginnerrrrrr

Reputation: 595

Changing the background color of UITableViewHeaderFooterView is not supported. Use the background view configuration instead

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.

enter image description here

enter image description here

these property are not work for me.

Upvotes: 0

Views: 3435

Answers (5)

Srinivasan_iOS
Srinivasan_iOS

Reputation: 1078

This is working fine...

  1. Change your FooterView (Xib) content view color to Default
  2. Change Inside FooterView (Xib) view color to Default
  3. FooterView (Xib) Inside any button, you can set the background color otherwise, you can set the background color at the code level.

Upvotes: 0

dklt
dklt

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

Naresh
Naresh

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

apetrov
apetrov

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

Sandeep Bhandari
Sandeep Bhandari

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

Related Questions