Search code examples
iosuitableviewuiappearance

iOS: wrong UITextField color of selected UITableViewCell


I have an issue with UITextField color which belongs to UITableViewCell when it's selected.

I have changed default UITableViewCell selection color using the following code that is being called from AppDelegate when application is launched with parameters:

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

This code was taken from the Internet and from one of StackOverflow's answers.

Everything is fine with normal cells with labels. But I have an issue with UITableViewCells that have UITextField inside it. When cell just has been selected selected, it's highlighted with green and text field looks fine. But other cells, that were selected previously (in selected state) have an issue with text field color. It's clear on the pictures below. enter image description here I have checked UI Hierarchy, and printed description of all three views, and all of them have the same description: 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>>

Solution

  • As per KyleH's advice, I have added the following code to my table view with UITextField inside:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        let cell = tableView.cellForRow(at: indexPath) as! CommonMenuTableViewCell
        cell.itemPriceTextField.backgroundColor = ColorThemes.uiTextFieldBackgroundColor
    
    }
    

    I've set desired color to UITextField background color property.