UITableView header resulting from -titleForHeaderInSection: has wrong text color in dark mode

In an iOS app i've been working on, I have a UITableview with headers. Headers are creating using:


-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   return [self.headerTitles objectAtIndex:section];
}


When I put the iOS simulator in dark mode, everything goes dark, but the text color in the headers remains black. Doesn't seem to be the case with a new stock project, but I'm wondering if there's something in this project that's making the text color not change properly. workaround is to implement:


//
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *headerTitle = [self.headerTitles objectAtIndex:section];
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderID"];
    headerView.textLabel.text = headerTitle;
    if (@available(iOS 13.0, *))
    {
        headerView.textLabel.textColor = UIColor.labelColor;
    }
    return headerView;
}


Anyone run into this and have an idea why I have to do this?

Replies

Heh, if I scroll the table view to take a header view off screen, then scroll it back on screen, it does come back with the correct appearance when it comes out of the reuse queue, if using the grouped table stye (not so with sticky headers)...initial load it has the wrong text color though.


Checked out the values here:


-(void)tableView:(UITableView*)tableView willDisplayHeaderView:(UIView*)view forSection:(NSInteger)section
{
    UIUserInterfaceStyle style = headerView.textLabel.traitCollection.userInterfaceStyle;
     NSLog(@"%li. %@",style,headerView.textLabel.textColor);
     //Logs out 2. 
}


Which looks right. Not sure why it's drawing the wrong text color.


Another workaround:


-(void)tableView:(UITableView*)tableView willDisplayHeaderView:(UIView*)view forSection:(NSInteger)section
{
        if ([view isKindOfClass:[UITableViewHeaderFooterView class]])
        {
            UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView*)view;
            headerView.backgroundView.backgroundColor = UIColor.secondarySystemBackgroundColor;
            headerView.textLabel.textColor = UIColor.labelColor;
        }
    
}


Guess I'm just going to use the workaround. Already spent way too much time on this.