Button switch change default color

4k views Asked by At

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.

2

There are 2 answers

0
TotoroTotoro On

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.

- (void)onFlipSwitch:(UISwitch *)aSwitch {
    if(aSwitch.on) {
        aSwitch.backgroundColor = [UIColor greenColor];
    }
    else {
        aSwitch.backgroundColor = [UIColor whiteColor];
    }
}
0
The Windwaker 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];