Search code examples
iosios6ios7uitextfield

UITextField: change appearance for disabled textfields


I'd like to change the appearance of disabled textfields, i.e. text and background color - is there an easy and legal way to achieve this?


Solution

  • You could subclass your UITextfield and overwrite the enabled property like this

    - (void)setEnabled:(BOOL)enabled
    {
        [super setEnabled:enabled];
        // Do your customization here, eg:
        self.backgroundColor = [UIColor blackColor];
    }
    

    Swift version:

    override var isEnabled: Bool {
        willSet {
            backgroundColor = newValue ? UIColor.white : UIColor.black
        }
    }