iOS: неправильный цвет UITextField для выбранного UITableViewCell - PullRequest
0 голосов
/ 28 августа 2018

У меня проблема с цветом 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

Этот код был взят из Интернета и одного из ответов StackOverflow.

Все нормально с нормальными ячейками с метками. Но у меня есть проблема с UITableViewCells, которые имеют UITextField внутри него. Когда ячейка была выбрана, она подсвечивается зеленым, и текстовое поле выглядит хорошо. Но другие ячейки, которые были выбраны ранее (в выбранном состоянии), имеют проблему с цветом текстового поля. Это видно по картинкам ниже. enter image description here Я проверил иерархию пользовательского интерфейса и напечатал описание всех трех представлений, и все они имеют одинаковое описание: 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 голосов
/ 30 августа 2018

Согласно совету KyleH, я добавил следующий код в мое табличное представление с UITextField внутри:

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

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

}

Я установил желаемый цвет для свойства цвета фона UITextField.

...