How do I disable a UISwitch?

Pooja picture Pooja · Apr 14, 2011 · Viewed 20.8k times · Source

Is this possible to disable a UISwitch? I do not mean putting it in an OFF state, I mean disabling user interaction, and having it appear gray.

In my app I have two conditions

if (condition == true) {  
  // UISwitch should be enabled  
} else {  
  // UISwitch should be visible, but disabled  
  // e.g uiswitch.enable=NO;  
} 

Any suggestions?

Answer

Mark Granoff picture Mark Granoff · Apr 14, 2011

This should do it:

switch.enabled = NO;

or equivalently:

[switch setEnabled:NO];

where switch is whatever your UISwitch variable name is.

Edit 18-Apr-2018

The answer above is (clearly) an Objective-C solution, written well before anyone had ever heard of Swift. The Swift equivalent solution is, of course:

switch.isEnabled = false