Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Label of UISwitch

Tags:

ios

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

like image 648
iamsult Avatar asked Jun 11 '09 13:06

iamsult


People also ask

What is use of UISwitch?

Overview. The UISwitch class declares a property and a method to control its on/off state. As with UISlider , when the user manipulates the switch control (“flips” it), it triggers the valueChanged event. You can customize the appearance of the switch by changing the color used to tint the switch when it is on or off.

What is a Ui switch?

Toggle switch (known as “toggles”) is a UI control that has two mutually-exclusive states, such as ON and OFF. The design and functionality of this control is based on a physical switch that allows users to turn things ON or OFF (i.e. light switch).


2 Answers

you can use images for on and off

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

image size is of 77*27

like image 140
Manjit Singh Avatar answered Sep 29 '22 19:09

Manjit Singh


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.

like image 41
quellish Avatar answered Sep 26 '22 19:09

quellish