我想在关闭状态下更改UISwitch中onTintColor的颜色.交换机在tableview中,并以编程方式进行切换.
[settingsSwitch setBackgroundColor:[UIColor whiteColor]]; [settingsSwitch setTintColor:[UIColor whiteColor]]; [settingsSwitch setThumbTintColor:[UIColor redColor]]; [settingsSwitch setOnTintColor:[UIColor colorWithRed:138/256.0 green:9/256.0 blue:18/256.0 alpha:1]];
这是我将背景颜色设置为白色时得到的结果.
没有背景我会得到红色,这是我的细胞的颜色.
这是我想要的结果,当开关打开onTintColor应该是深红色,而在关闭状态它应该是白色.
我尝试用这行代码在开关上设置图像
[settingsSwitch setOnImage:[UIImage imageNamed:@"on.png"]]; [settingsSwitch setOffImage:[UIImage imageNamed:@"off.png"]];
但它没有改变图像.
我想改变开关处于关闭状态的颜色.
希望我能清楚地解释我的问题.谢谢你提前帮助.
解决方法
您可以使用以下CODE来满足要求.
你的ViewDidLoad
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. [self.view setBackgroundColor:[UIColor redColor]]; settingsSwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this if (settingsSwitch.on) { NSLog(@"If body "); [settingsSwitch setThumbTintColor:[UIColor redColor]]; [settingsSwitch setBackgroundColor:[UIColor whiteColor]]; [settingsSwitch setOnTintColor:[UIColor whiteColor]]; }else{ NSLog(@"Else body "); [settingsSwitch setTintColor:[UIColor clearColor]]; [settingsSwitch setThumbTintColor:[UIColor redColor]]; [settingsSwitch setBackgroundColor:[UIColor colorWithRed:138/256.0 green:9/256.0 blue:18/256.0 alpha:1]]; } }
- (IBAction)switchStatusChange:(UISwitch *)sender { if (sender.on) { NSLog(@"If body "); [sender setThumbTintColor:[UIColor redColor]]; [sender setBackgroundColor:[UIColor whiteColor]]; [sender setOnTintColor:[UIColor whiteColor]]; }else{ NSLog(@"Else body "); [sender setTintColor:[UIColor clearColor]]; [sender setThumbTintColor:[UIColor redColor]]; [sender setBackgroundColor:[UIColor colorWithRed:138/256.0 green:9/256.0 blue:18/256.0 alpha:1]]; } }