Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to disable a UITextField?

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

like image 542
CC. Avatar asked Mar 01 '09 12:03

CC.


People also ask

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.


2 Answers

In Objective-C:

textField.enabled = NO; 

In Swift:

textField.isEnabled = false 

Reference: UIControl.isEnabled

like image 120
Joel Levin Avatar answered Sep 30 '22 20:09

Joel Levin


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]; 
like image 36
Mariana B. Avatar answered Sep 30 '22 18:09

Mariana B.