【发布时间】:2013-12-24 06:49:57
【问题描述】:
如何更改 UISwitch 按钮的颜色?当它出现在模拟器中时,它不应该在On,而是在Off。我想看到它是白色的,当它被激活时,它应该变成绿色。
【问题讨论】:
如何更改 UISwitch 按钮的颜色?当它出现在模拟器中时,它不应该在On,而是在Off。我想看到它是白色的,当它被激活时,它应该变成绿色。
【问题讨论】:
创建一个用于更改开关值的处理程序(见下文)。另外别忘了根据开关的状态设置初始颜色。
- (void)onFlipSwitch:(UISwitch *)aSwitch {
if(aSwitch.on) {
aSwitch.backgroundColor = [UIColor greenColor];
}
else {
aSwitch.backgroundColor = [UIColor whiteColor];
}
}
【讨论】:
您可以访问 UISwitchView 的 onTintColor 和 tintColor 属性。
@property(nonatomic, retain) UIColor *onTintColor
@property(nonatomic, retain) UIColor *tintColor
初始化开关时,将 onTintColor 设置为您想要的颜色为 On 值,将 tintColor 设置为 Off 值。 例如在 UIViewController 中:
UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 41.0, 30.0)];
// on color
theSwitch.onTintColor = [UIColor blueColor];
//off color
theSwitch.tintColor = [UIColor redColor];
[self.view addSubview:theSwitch];
【讨论】: