19

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?

Micah Hainline
  • 14,367
  • 9
  • 52
  • 85
Pooja
  • 2,162
  • 5
  • 33
  • 64

4 Answers4

47

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

Mark Granoff
  • 16,878
  • 2
  • 59
  • 61
7

Yes you can. UISwitch inherits from UIControl, and UIControl has an enabled property. Apple's UIControl Documentation has all of the details.

To enable

switch.enabled = YES;

To disable

switch.enabled = NO;
odrm
  • 5,149
  • 1
  • 18
  • 13
5

For those looking for Swift 3,

switch.isEnabled = false // Disabled switch

I know you didn't ask for "off" state, but just in case anybody, like myself, stumbled upon here :

switch.isOn = false
KMC
  • 1,677
  • 3
  • 26
  • 55
-6

[switchName enabled] = NO;

Use that to disable your switch.

EDIT thanks to rckoenes: "You should not try to set a property via getter. You should use either the setter of . syntax property."

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • 1
    You should not try to set a property via getter. You should use either the setter of . syntax property. – rckoenes Apr 14 '11 at 13:52