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.
可以禁用UISwitch吗?我不是说把它放在关闭状态,我是说禁用用户交互,让它显示为灰色。
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?
有什么建议吗?
4 个解决方案
#1
36
This should do it:
这应该这样做:
switch.enabled = NO;
or equivalently:
或者说:
[switch setEnabled:NO];
where switch
is whatever your UISwitch
variable name is.
这里的switch是UISwitch变量的名称。
Edit 18-Apr-2018
编辑18 - 4月- 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:
上面的答案(很明显)是一个Objective-C的解决方案,在任何人听说Swift之前就已经写好了。当然,Swift等价的解决方案是:
switch.isEnabled = false
开关。isEnabled = false
#2
7
Yes you can. UISwitch
inherits from UIControl
, and UIControl
has an enabled
property. Apple's UIControl Documentation has all of the details.
是的,你可以。UISwitch继承自UIControl, UIControl有一个enabled属性。苹果的UIControl文档有所有的细节。
To enable
要启用
switch.enabled = YES;
To disable
禁用
switch.enabled = NO;
#3
1
For those looking for Swift 3,
对于那些寻找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
#4
-6
[switchName enabled] = NO;
[switchName启用]=没有;
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."
感谢rckoenes:“您不应该尝试通过getter来设置属性。你应该使用。语法属性。”
#1
36
This should do it:
这应该这样做:
switch.enabled = NO;
or equivalently:
或者说:
[switch setEnabled:NO];
where switch
is whatever your UISwitch
variable name is.
这里的switch是UISwitch变量的名称。
Edit 18-Apr-2018
编辑18 - 4月- 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:
上面的答案(很明显)是一个Objective-C的解决方案,在任何人听说Swift之前就已经写好了。当然,Swift等价的解决方案是:
switch.isEnabled = false
开关。isEnabled = false
#2
7
Yes you can. UISwitch
inherits from UIControl
, and UIControl
has an enabled
property. Apple's UIControl Documentation has all of the details.
是的,你可以。UISwitch继承自UIControl, UIControl有一个enabled属性。苹果的UIControl文档有所有的细节。
To enable
要启用
switch.enabled = YES;
To disable
禁用
switch.enabled = NO;
#3
1
For those looking for Swift 3,
对于那些寻找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
#4
-6
[switchName enabled] = NO;
[switchName启用]=没有;
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."
感谢rckoenes:“您不应该尝试通过getter来设置属性。你应该使用。语法属性。”