How to set On Image and Off Image for UISwitch in storyBoard?

6.1k views Asked by At

I am working on UISwitch, i want to set on Image and off Image for UISwitch in storyboard but image is not set on switch.

I try this : enter image description here

I want to create like this:

enter image description here

Not like this --->

enter image description here

Thanks in advance.

2

There are 2 answers

1
Shilpa M On

You can detect the switch on/off state programmatically and set image.

[YourSwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];

    - (void) switchToggled:(id)sender {
        UISwitch *mySwitch = (UISwitch *)sender;
        if ([mySwitch isOn]) {
            [switchControl setThumbTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Thumb.png"]]];
        } else {
            [switchControl setThumbTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Thumb.png"]]];
        }
    }
0
Anjali jariwala On

You can use UIButton in place of UISwitch as its useful and also time saving and easy to customise..

use image slicing for UIButton like you are using UISwitch and set it as Background image like this

[cell.btnSwitch setBackgroundImage:[UIImage imageNamed:@"switchOn"] forState:UIControlStateNormal];
[cell.btnSwitch setBackgroundImage:[UIImage imageNamed:@"switchOff"] forState:UIControlStateNormal];