Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable a UISwitch?

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?

like image 594
Pooja Avatar asked Apr 14 '11 13:04

Pooja


People also ask

How do I disable UISwitch?

switch. enabled = NO; or equivalently: [switch setEnabled:NO];

How to disable switch in ios?

1 Open your iPhone's Settings. 2 Scroll down and tap "Accessibility." 3 Scroll down and tap " Switch Control." 4 Toggle the switch to Off.

What is use of UISwitch?

Overview. The UISwitch class declares a property and a method to control its on/off state. As with UISlider , when the user manipulates the switch control (“flips” it), it triggers the valueChanged event. You can customize the appearance of the switch by changing the color used to tint the switch when it is on or off.

What is a Ui switch?

Toggle switch (known as “toggles”) is a UI control that has two mutually-exclusive states, such as ON and OFF. The design and functionality of this control is based on a physical switch that allows users to turn things ON or OFF (i.e. light switch).


2 Answers

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

like image 56
Mark Granoff Avatar answered Oct 15 '22 18:10

Mark Granoff


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;
like image 37
odrm Avatar answered Oct 15 '22 18:10

odrm