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

5.6k views Asked by At

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 ?

3

There are 3 answers

0
Helga Sokolova On BEST ANSWER

This works for me.

v.contentView.backgroundColor = .blue

instead of

v.backgroundView.backgroundColor = .blue
0
Jabson On

Try to add overlay view and change this color for this view.

UIView *coloredView = [[UIView alloc] init];
coloredView.backgroundColor = [UIColor blueColor];
[v addSubview:coloredView];

[[coloredView.leadingAnchor constraintEqualToAnchor:v.leadingAnchor constant:0] setActive:YES];
[[coloredView.trailingAnchor constraintEqualToAnchor:v.trailingAnchor constant:0] setActive:YES];
[[coloredView.topAnchor constraintEqualToAnchor:v.topAnchor constant:0] setActive:YES];
[[coloredView.bottomAnchor constraintEqualToAnchor:v.bottomAnchor constant:0] setActive:YES];
4
adriaan On

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
    }
}