7

I have to change the label of a UISwitch from ON-OFF to YES-NO.

I want this method to be implemented in separate class and then accessed by other classes.

I have tried to implement the snippets provided in the cook book, but without success

James Webster
  • 31,873
  • 11
  • 70
  • 114
iamsult
  • 1,581
  • 1
  • 15
  • 21
  • See this http://stackoverflow.com/questions/2711180/changing-the-text-on-a-uiswitch Hope to help you, Good luck. – zhocker Feb 05 '14 at 07:55

2 Answers2

1

you can use images for on and off

@property(nonatomic, retain) UIImage *offImage;
@property(nonatomic, retain) UIImage *onImage;

image size is of 77*27

Pawan Sharma
  • 3,199
  • 1
  • 25
  • 32
Manjit Singh
  • 238
  • 2
  • 10
1

UISwitch uses images for drawing. To change the text of a UISwitch, you would have to set the onImage and offImage properties of the UISwitch to use images with your custom text. This could be done directly on a UISwitch instance, or using UIAppearance to set your custom image across all UISwitch instances in your app:

[[UISwitch appearance] setOnImage:onImage];
[[UISwitch appearance] setOffImage:offImage];

Unfortunately, setting custom on and off images for UISwitch is not functional in iOS 7 or later. From the documentation:

In iOS 7, this property has no effect. In iOS 6, this image represents the interior contents of the switch. The image you specify is composited with the switch’s rounded bezel and thumb to create the final appearance.

And it has not been marked as deprecated. In iOS 8 this still seems to be the case, unfortunately. Customizing the colors of a UISwitch still works, but using custom images does not. To customize the images (and thus text) of a switch you will have to use a custom control class.

quellish
  • 21,123
  • 4
  • 76
  • 83