Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: the background color of the header of my TableView is not changing anymore in iOS13

My TableView's header is not displaying well in iOS13. No matter what color I put, it always displays a light gray now...

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{   //Section color & style

    UITableViewHeaderFooterView *v = (UITableViewHeaderFooterView *)view;

    v.backgroundView.alpha = 1;
    v.textLabel.textColor = sectionColor;
    v.textLabel.font = sectionFont;
    v.textLabel.numberOfLines = 1;
    v.textLabel.minimumScaleFactor = 0.5;
    v.textLabel.adjustsFontSizeToFitWidth = YES;
    v.backgroundView.backgroundColor = [UIColor blueColor];
} 

iOS12:
iOS12

iOS13: iOS13

It is strange because when I put a stop in the debugger in step by step, it displays me the good image in iOS13, but not in the app:

stepbystep Any suggestions, thanks in advance ?

like image 250
ΩlostA Avatar asked Sep 09 '19 10:09

ΩlostA


2 Answers

I was noticing the same thing in one of my apps. Then saw a log message in the console:

Setting the background color on UITableViewHeaderFooterView has been deprecated. Please set a custom UIView with your desired background color to the backgroundView property instead.

Setting a custom UIView with the desired background color as the backgroundView of the UITableViewHeaderFooterView solved the problem.

Code sample

class SomeHeaderView: UITableViewHeaderFooterView {

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func configure() {
        let backgroundView = UIView(frame: .zero)
        backgroundView.backgroundColor = .blue
        self.backgroundView = backgroundView
    }
}
like image 190
adriaan Avatar answered Nov 10 '22 19:11

adriaan


This works for me.

v.contentView.backgroundColor = .blue

instead of

v.backgroundView.backgroundColor = .blue
like image 8
Helga Sokolova Avatar answered Nov 10 '22 18:11

Helga Sokolova