iamsult
iamsult

Reputation: 1579

Change Label of UISwitch

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

Upvotes: 7

Views: 14730

Answers (2)

quellish
quellish

Reputation: 21254

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.

Upvotes: 1

Manjit Singh
Manjit Singh

Reputation: 238

you can use images for on and off

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

image size is of 77*27

Upvotes: 1

Related Questions