How to change UISwitch default color for OFF state?

余生颓废 提交于 2019-12-06 03:58:17

问题


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

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

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

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.


回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/25616900/how-to-change-uiswitch-default-color-for-off-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!