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
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.
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).
you can use images for on
and off
@property(nonatomic, retain) UIImage *offImage;
@property(nonatomic, retain) UIImage *onImage;
image size is of 77*27
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With