49

Is there an easy way to disable a UITextField in code?

My app has 12 UITextField that are all turned on by default, but when a change is detected in my Segment Control I want to disable some of the UITextField depending on what Segment the user picks.

Just need to know how to disable it or make it non-editable?

Thanks

seggy
  • 1,176
  • 2
  • 18
  • 38
CC.
  • 793
  • 2
  • 9
  • 13

5 Answers5

114

In Objective-C:

textField.enabled = NO;

In Swift:

textField.isEnabled = false

Reference: UIControl.isEnabled

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Joel Levin
  • 2,878
  • 1
  • 21
  • 13
  • 1
    lol, was sure I tried this all-ready, but I must have made a typo or something because it works.. your a lifesaver... Thanks :) – CC. Mar 01 '09 at 12:42
  • 6
    If you're ever trying to find out *if* its enabled, keep in mind the getter is textField.isEnabled, not textField.enabled. – mk12 Aug 01 '09 at 20:40
  • The property `enabled` inherits UIControl's property. if NO, ignores touch events. it's a good work around approach. – Allen Jan 28 '15 at 12:15
30

If you also want to indicate that the text field is disabled you should add more code:

if using UITextBorderStyleRoundedRect, you can only gray out text:

textField.enabled = NO;
textField.textColor = [UIColor lightGrayColor];

if using any other style, you can also gray out background:

textField.enabled = NO;
textField.backgroundColor = [UIColor lightGrayColor];
Mariana B.
  • 437
  • 5
  • 14
12

This would be the simplest of all , when you multiple textfields too:

in viewDidLoad:(set the delegate only for textfields which should not be editable.

self.textfield.delegate=self;

and insert this delegate function:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}

Thats it!

Ankish Jain
  • 11,305
  • 5
  • 36
  • 34
  • 1
    How is this simpler than `textField.enabled = NO`, which was already posted? I didn't -1 this, because it's a legitimate alternate solution that might be more convenient in a couple of edge cases (e.g. if you've got some object that's the delegate of loads of different text fields, and which ones are disabled is changing frequently), but for most scenarios it's definitely not simpler than the accepted answer. – Mark Amery Aug 11 '13 at 10:37
  • You can always check this way: 'code' if(textfield==self.aTextField){ //check if thats the textfield u want to disable return NO; else{ – Ankish Jain Aug 13 '13 at 12:27
  • 1
    This is the best solution when you have multiple textfield! Thank you Ankish – FouZ Aug 07 '15 at 00:40
  • This is also the way to go if you don't want the UITextField to be grayed out. – Jonathan Cabrera Jan 10 '19 at 22:44
2

Make category on UIView.

add following methods in that category.

-(void)disable {
    self.alpha = 0.5;
    self.userInteractionEnabled = FALSE;
}

-(void)enable {
    self.alpha = 1.0;
    self.userInteractionEnabled = TRUE;
}

then just call

yourTextField.disable();

Actually,You can use this on any view that you want.

Bhavesh
  • 1,422
  • 1
  • 12
  • 31
-1

You should conform to UITextFieldDelegate and return false in this method. This way the text field is still selectable, but it is not available for editing and cutting

Code is in Swift 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  return false
}
onmyway133
  • 45,645
  • 31
  • 257
  • 263
  • Actually, it will be still available for everything. The keyboard will open and will accept key-presses, you will get a paste action but text field will ignore it all. From user perspective a terrible solution. – Sulthan May 18 '18 at 12:44
  • Sometimes if you want the textfield to open a UIPickerView, this would be a viable solution – Huy-Anh Hoang Feb 25 '20 at 19:56