Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 365
swalkner Avatar asked Jan 21 '14 13:01

swalkner


People also ask

How do I disable TextField editing in Swift?

Set the Boolean variable to false, which enables editing in the text field. When someone taps the Done button, isEditing becomes false. Set the Boolean variable to true, which disables editing in the text field.

How do I disable TextField?

Use the enabled: property of the TextField widget by setting it to false : TextField( enabled: false, ... ) This field won't respond to onTap events - it is similar to a disabled field in HTML.

What is Uitextfield in Swift?

An object that displays an editable text area in your interface.


1 Answers

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
    }
}
like image 111
NoilPaw Avatar answered Sep 21 '22 18:09

NoilPaw