17

The UISwitch currently says ON and OFF. Can I change the text to YES and NO?

Would it be hard? Or do I just rephrase the question I ask the user?

teedyay
  • 23,293
  • 19
  • 66
  • 73
Sam Jarman
  • 7,277
  • 15
  • 55
  • 100
  • The UISwitch doesn't need to say ON/OFF. It can be オン/オフ or even two graphics depending on the locale. – kennytm Apr 26 '10 at 05:16
  • 13
    @Marcelo Cantos: Heh - that's how I got here. :-) – teedyay Apr 28 '11 at 12:49
  • useful links for custom switch http://www.cocoacontrols.com/platforms/ios/controls/dcroundswitch http://www.cocoacontrols.com/platforms/ios/controls/simpleswitch – junaidsidhu Oct 30 '12 at 11:40
  • Yet another customizable UISwitch alternative: https://github.com/alexnauda/ERScrollSwitch – Alex Nauda Sep 25 '13 at 18:34
  • [Domestic Cat](http://domesticcat.com.au/) has published a custom controller that allows for custom labels and frame sizing. It is released under the MIT license and simply requires inclusion of QuartzCore in your project. You can grab the [DCRoundedSwitch](https://github.com/domesticcatsoftware/DCRoundSwitch) controller from git hub. – Jason George Nov 01 '11 at 19:54

3 Answers3

11

As of iOS 6, you can set

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

Docs say:

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.

The size of this image must be less than or equal to 77 points wide and 27 points tall. If you specify larger images, the edges may be clipped.

c roald
  • 1,984
  • 1
  • 20
  • 30
11

I've done exactly this in iOS 6 with onImage and offImage, here are the images I used:

enter image description here

enter image description here

enter image description here

enter image description here

Allen Zeng
  • 2,635
  • 2
  • 20
  • 31
-26

To change the text of an UISwitch e.g. to "Foo/Bar", do the following:

((UILabel *)[[[[[[_agreeAgb subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"Foo";
((UILabel *)[[[[[[_agreeAgb subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"Bar";
oldbread
  • 17
  • 3
  • 6
    This code makes assumptions on the view hierarchy without any verifications whatsoever - don't use it! For more details: http://stackoverflow.com/questions/4806743/changing-of-uiswitch-text-in-ios-4-2 – André Morujão Jul 11 '11 at 16:50
  • 6
    will crash in newer iOS like 4.2 and later – Sedat Kilinc Sep 25 '11 at 03:07
  • 9
    this is a bad answer, its best to create a custom UISwitch .. the above might not work under future versions of iOS – Marty Nov 09 '11 at 00:46