5

I want to change the color of onTintColor in the UISwitch for the off State. The switch is in tableview and switch is made programmatically.

    [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]];

enter image description here

This is the result i get when i set the background color white.

enter image description here

And without background i get red color which is the color of my cell.

enter image description here

and this is the result i want, when switch is on the onTintColor should b dark red, and in off state it should be white.

i tried setting the image on switch with this line of code

[settingsSwitch setOnImage:[UIImage imageNamed:@"on.png"]];
[settingsSwitch setOffImage:[UIImage imageNamed:@"off.png"]];

But its not changing the images. i want to change the color of switch in off state. Hope i have explained my question clearly.thanks for helping in advance.

4
  • Not sure if that is a good idea. It looks like the on switch is off. And the off switch looks like it's on. (Because it has a bright, white, active looking color).
    – Marc
    Sep 2, 2014 at 6:29
  • 2
    I agree but that's the requirement of the client.
    – Sam Shaikh
    Sep 2, 2014 at 6:43
  • @iOSDeveloper, Client is always right :) Sep 2, 2014 at 7:42
  • Is it possible to give border to the switch Jul 16, 2015 at 16:22

3 Answers 3

6

You can use following CODE to fulfill requirement.

Your 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]];
    }
}

Method where status change IBAction is called.

- (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]];
    }
}
8
  • This actually works, but it screws with the thumb animation (to left or right). Any workarounds?
    – Legoless
    Oct 2, 2014 at 7:14
  • how could we make it smooth Jul 16, 2015 at 15:59
  • it works but not giving smooth animation. any way to solve this? Veer bro.
    – MRizwan33
    Mar 20, 2018 at 11:10
  • 2
    @MRizwan33 try wrapping in dispatch queue main async
    – user5306470
    Nov 22, 2018 at 3:13
  • 1
    @NabeelAhmed ok awesome! Yeah always update UI on main thread. User shouldn't have to deal with unresponsive interface.
    – user5306470
    Nov 23, 2018 at 5:58
4

You can use you first approach + add border radius to remove unwanted corners:

slider.backgroundColor = [UIColor whiteColor];
slider.layer.cornerRadius = 18.0;

Originally from this answer.

1
  • cornerRadius affect in the border corner, its a poor solution Aug 24, 2016 at 21:53
1

corner in UISwitch cut the view, and isn't see rounded, the best answer is here https://stackoverflow.com/a/38564168/6609238

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.