3

Is possible to add images to an UISwitch background,like when the state is ON (be one background) and when the state is OFF (another background image)?

Mateus
  • 2,640
  • 5
  • 44
  • 62

3 Answers3

2

To change the background color (not image) all you have to do is below. This changes the off color for all UISwitch controls in realm.

[[UISwitch appearance] setTintColor:[UIColor brownColor]];//Off Color
_locationSwitch.onTintColor = [UIColor orangeColor];//On Color

If you want to use image, use the following cool hack of using the segmented control. https://stackoverflow.com/a/5088099/1705353

If you want to write a lot of code, you can write your own control. Valeriy Van pointed to: https://github.com/homick/iPhone-Snippets/tree/master/General

Also another resource is: http://www.raywenderlich.com/23424/photoshop-for-developers-creating-a-custom-uiswitch with code at: http://cdn2.raywenderlich.com/wp-content/uploads/2012/10/CustomSwitchResources.zip

Community
  • 1
  • 1
Dickey Singh
  • 713
  • 1
  • 8
  • 16
0

You can recreate a custom UISwitch by subclassing UIControl. By doing this you can have full control over what the switch looks like. You can look take a look at SevenSwitch. A custom UISwitch replacement I've created. The on/off colors can be customized to your liking.

https://github.com/bvogelzang/SevenSwitch

bvogelzang
  • 1,405
  • 14
  • 17
  • Please disclose any [affiliations](https://stackoverflow.com/help/promotion) and do not use the site as a way to promote your site through posting. See [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). –  Feb 26 '19 at 05:47
0

I would like to suggest you to follow Custom UISwitch & App Store approval to have a better idea, which is suggesting you to create a custom switch control.

Community
  • 1
  • 1
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54