5

自 2011 年以来,我一直在为我的医疗应用程序使用 Objective-C 代码,它基本上有 5 个带有关联 UITableViews 的选项卡,其中 3 个使用自定义单元格。最初使用暗模式时,我发现自定义单元格不会自动更改为暗模式。相反,我需要实现下面的代码来测试暗模式并相应地更改单元格文本和背景。

问题是第一个选项卡中的空单元格不会发生暗模式更改,填充单元格的上方或下方;它们仍然是空白的白色。但是,与第二个 2 个选项卡关联的 UITableViews 中类似的空单元格在暗模式下的行为与预期相同。

下面的示例图像显示了第一个和第三个选项卡显示的单元格。我现在想知道操作系统中是否存在错误,或者我是否没有实施适当的更改,以解释为什么暗模式仅在第一个选项卡中无法正常工作。

- (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];

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

13

您需要使用正确的颜色,即在明暗模式之间自动适应的颜色。

whiteColor避免使用和之类的颜色blackColor。使用labelColorsystemBackgroundColor。将这些颜色用于单元格和表格视图。然后,您不需要任何代码来检查当前特征或界面样式。

代码如:

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.calculationLabel.textColor = UIColor.labelColor;
cell.calculationLabel.backgroundColor = UIColor.systemBackgroundColor;
cell.statusLabel.backgroundColor = UIColor.systemBackgroundColor;
cell.favoriteStatus.backgroundColor = UIColor.systemBackgroundColor;
cell.backgroundColor = UIColor.systemBackgroundColor;
于 2019-09-13T01:12:50.073 回答