How can I change the color of the UISwitch Button? When it appears in the simulator, it shouldn't be in On
, but in Off
. I want to see it in white color, and when active, it should become green.
Button switch change default color
4k views Asked by user3075385 At
2
There are 2 answers
0
On
You can have access to the onTintColor and tintColor property of the UISwitchView.
@property(nonatomic, retain) UIColor *onTintColor
@property(nonatomic, retain) UIColor *tintColor
When initializing your switch, set the onTintColor for the color you want for On value and tintColor for Off value. For instance in a 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];
Create a handler for changing the switch value (see below). Also don't forget to set the initial color, depending on the state of the switch.