iOS: 所选 UITableViewCell 的 UITextField 颜色错误

iOS: wrong UITextField color of selected UITableViewCell

我对 UITextField 颜色有疑问,它在被选中时属于 UITableViewCell。

我已经使用以下代码更改了默认 UITableViewCell 选择颜色,当使用参数启动应用程序时从 AppDelegate 调用该代码:

UITableView.appearance().backgroundColor = backgroundColor
UITableViewCell.appearance().backgroundColor = backgroundColor
UITableViewCell.appearance().tintColor = tintColor
let colorView = UIView()
colorView.backgroundColor = ColorThemes.uiTableViewCellSelectedBackgroundColor
UITableViewCell.appearance().selectedBackgroundView = colorView

UITextField.appearance().textColor = ColorThemes.textColorNormal
UITextField.appearance().backgroundColor = ColorThemes.uiTextFieldBackgroundColor
UITextField.appearance().tintColor = UIColor.gray
UITextField.appearance().keyboardAppearance = ColorThemes.uiKeyboardAppearance

此代码取自互联网和 Whosebug 的一个答案。

带有标签的正常细胞一切正常。但是我对 UITableViewCells 有一个问题,里面有 UITextField。当单元格刚刚被选中时,它以绿色突出显示,文本字段看起来很好。 但是之前选择的其他单元格(处于选定状态)在文本字段颜色方面存在问题。下面的图片就很清楚了。 我检查了 UI 层次结构,并打印了所有三个视图的描述,它们都具有相同的描述:

Printing description of :
<_UITextFieldRoundedRectBackgroundViewNeue: 0x7fdeb209ac80; frame = (0 0; 70 30); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; tintColor = UIExtendedGrayColorSpace 1 1; layer = <CALayer: 0x60000280d980>>
Printing description of :
<_UITextFieldRoundedRectBackgroundViewNeue: 0x7fdeaedb2b30; frame = (0 0; 70 30); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; tintColor = UIExtendedGrayColorSpace 1 1; layer = <CALayer: 0x600002801180>>
Printing description of :
<_UITextFieldRoundedRectBackgroundViewNeue: 0x7fdeb209e090; frame = (0 0; 70 30); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; tintColor = UIExtendedGrayColorSpace 1 1; layer = <CALayer: 0x60000287ac60>>

根据 KyleH 的建议,我已将以下代码添加到我的 table 视图中,其中包含 UITextField:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath) as! CommonMenuTableViewCell
    cell.itemPriceTextField.backgroundColor = ColorThemes.uiTextFieldBackgroundColor

}

我已将所需颜色设置为 UITextField 背景色 属性。