【问题标题】:Custom cell in UITableView not working properly in iOS 13 Dark ModeUITableView 中的自定义单元格在 iOS 13 暗模式下无法正常工作
【发布时间】:2020-01-14 21:16:50
【问题描述】:

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

【问题讨论】:

    标签: objective-c uitableview cell ios13 ios-darkmode


    【解决方案1】:

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

    避免使用whiteColorblackColor 之类的颜色。使用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;
    

    【讨论】:

    • 我替换了您提供的代码,但不幸的是,它重现了我试图解决的相同暗模式行为。也就是说,tableview 的未占用部分保持白色,但仅在第一个选项卡下。在暗模式下,应用程序的第二个和第三个选项卡中未占用的 tableview 空间适当地为黑色,使用与第一个选项卡中的 tableview 相同的代码。
    • 您需要设置表格视图的背景,而不仅仅是单元格。
    • 感谢您的帮助!表格视图的未占用部分现在根据暗模式状态适当地着色为白色或黑色,并添加以下代码:self.tableView.backgroundColor = [UIColor systemBackgroundColor];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多