Custom cell in UITableView not working properly in iOS 13 Dark Mode
Asked Answered
S

1

6

I have used, since 2011, Objective-C code for my medical application, which basically has 5 tabs with associated UITableViews, 3 of which use custom cells. Initially with using Dark Mode I found that the custom cells did not change to Dark Mode automatically. Instead, I needed to implement the code below to test for Dark Mode and make cell text and background changes accordingly.

The problem is that the Dark Mode changes do not occur for empty cells in the first tab, above or below the filled cells; they remain blank white. However, similar, empty cells in the UITableViews associated with the second 2 tabs DO behave as expected in Dark Mode.

The sample images below show the cells displayed by the first and third tabs. I'm wondering at this point if there is a bug in the OS, or whether I just am not implementing the proper change, to explain why Dark Mode does not work properly in the first tab only.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier "; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[CustomCell class]])
                cell = (CustomCell *)oneObject;
    } 

    NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];

    cell.calculationLabel.text = [dictionary objectForKey:@"Title"];
    [cell.calculationLabel setLineBreakMode:NSLineBreakByWordWrapping];
    cell.calculationLabel.numberOfLines = 0;

    //Make color changes in cells relevant to Dark mode, if present.
    if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { //Dark mode
        cell.calculationLabel.textColor = [UIColor whiteColor];
        cell.calculationLabel.backgroundColor = [UIColor blackColor];
        cell.statusLabel.backgroundColor = [UIColor blackColor];
        cell.favoriteStatus.backgroundColor = [UIColor blackColor];
        cell.backgroundColor = [UIColor blackColor];
    } else { //Light mode
        cell.calculationLabel.textColor = [UIColor blackColor];
        cell.calculationLabel.backgroundColor = [UIColor whiteColor];
        cell.statusLabel.backgroundColor = [UIColor whiteColor];
        cell.favoriteStatus.backgroundColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor whiteColor];
    }

    cell.statusLabel.textColor = [UIColor systemGreenColor];

enter image description here

enter image description here

Syconium answered 13/9, 2019 at 1:0 Comment(0)
K
15

You need to be using the proper colors, the ones that automatically adapt between light and dark mode.

Avoid colors like whiteColor and blackColor. Use labelColor and systemBackgroundColor. Use those colors for the cells and the table view. Then you don't need to have any code that checks the current traits or interface style.

Code such as:

if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { //Dark mode
    cell.calculationLabel.textColor = [UIColor whiteColor];
    cell.calculationLabel.backgroundColor = [UIColor blackColor];
    cell.statusLabel.backgroundColor = [UIColor blackColor];
    cell.favoriteStatus.backgroundColor = [UIColor blackColor];
    cell.backgroundColor = [UIColor blackColor];
} else { //Light mode
    cell.calculationLabel.textColor = [UIColor blackColor];
    cell.calculationLabel.backgroundColor = [UIColor whiteColor];
    cell.statusLabel.backgroundColor = [UIColor whiteColor];
    cell.favoriteStatus.backgroundColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor whiteColor];
}

becomes:

cell.calculationLabel.textColor = UIColor.labelColor;
cell.calculationLabel.backgroundColor = UIColor.systemBackgroundColor;
cell.statusLabel.backgroundColor = UIColor.systemBackgroundColor;
cell.favoriteStatus.backgroundColor = UIColor.systemBackgroundColor;
cell.backgroundColor = UIColor.systemBackgroundColor;
Konyn answered 13/9, 2019 at 1:12 Comment(3)
I substituted the code that you provided, but unfortunately it reproduced the same Dark Mode behavior that I am trying to solve. That is, the unoccupied portions of the tableview remain white, but only under the first tab. The unoccupied tableview space is appropriately black in the second and third tabs of the app when in Dark Mode, using the same code as the tableview in the first tab.Syconium
You need to set the table view's background, not just the cells.Konyn
Thanks for the help! The unoccupied portions of the tableview now are colored appropriately white or black, depending upon Dark Mode status, with this added code: self.tableView.backgroundColor = [UIColor systemBackgroundColor];Syconium

© 2022 - 2024 — McMap. All rights reserved.