iOS:选定的UITableViewCell的UITextField颜色错误

时间:2018-08-28 20:43:02

标签: ios uitableview uiappearance

我在选择UITableViewCell时遇到UITextField颜色的问题。

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

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

此代码是从Internet和StackOverflow的答案之一中获取的。

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

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

1 个答案:

答案 0 :(得分:0)

按照KyleH的建议,我在内部具有UITextField的表视图中添加了以下代码:

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

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

}

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

相关问题