Custom UISwitch with image

Arshad Parwez picture Arshad Parwez · Apr 29, 2013 · Viewed 36.3k times · Source

I need to implement a custom switch in a project. Currently what I have found is that we cannot alter with UISwitch in way to implement as shown in below image. I've followed posts and googled through stackoverflow and other blogs but didn't found a solution as desired. One way is to go with UISegmented control as in this post, but that too doesn't solves my problem.

enter image description here

Thanks in advance for any help

Answer

Odrakir picture Odrakir · Apr 29, 2013

As gasparuff says, you can also do it with a UIButton, just set the images:

[button setImage:[UIImage imageNamed:@"image_on"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"image_off"] forState:UIControlStateNormal];

and when it's tapped just toggle the selected property.

- (void) buttonTap {
    button.selected = !button.selected;
}

Which will change the images automatically.