1

I want to make custom UISwitch for project. I want to change text as well as background.

I gone through multiple posts on stack-overflow for this. Some where just image drawing or core graphics drawing. I don't want both of them as they are not sufficient.

  1. Custom UISwitch & App Store approval
  2. UISwitch - change from on/off to yes/no
  3. http://www.catamount.com/blog/uicustomswitch-customizing-uiswitch-color-it-change-labels/

But I need a single code which will be sufficient for iOS 4.2 as well as iOS 6.0.

Community
  • 1
  • 1
parilogic
  • 1,147
  • 9
  • 26

2 Answers2

2

Use this to change the background color . This changes the off color for UISwitch control

    // Set the tint color for the On state. Here we set green colour tint for On state
[customizedSwitch setOnTintColor:[UIColor colorWithRed:64.0/255 green:128.0/255.0 blue:64.0/255.0 alpha:1.0]];

// Set the tint color for the Off state. Here we set green red tint for Off state
[customizedSwitch setTintColor:[UIColor colorWithRed:255.0/255 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]];

// Set the tint color for the round shaped Thumb. Here we set blue tint for the Thumb
[customizedSwitch setThumbTintColor:[UIColor colorWithRed:64.0/255 green:64.0/255.0 blue:255.0/255.0 alpha:1.0]];

Note : To modify the private view hierarchy of framework controls is absolutely unsupported, and can cause incompatibility with OS updates if you try to change text using subviews of uiswitch.

Use this nice tutorial for both these tasks: customizing-user-interface-uiswitch Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • He asked for code working on iOS 4.2. `setOnTintColor`is iOS 5.0+, `thumbTintColor`is iOS 6.0+ – Vinzzz May 27 '13 at 13:13
  • How to set title without using image on iOS 6.0 ? – parilogic May 27 '13 at 13:58
  • You can use this : ((UILabel *)[[[[[[myswitch subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"YES"; ((UILabel *)[[[[[[myswitch subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"NO"; – Nishant Tyagi May 27 '13 at 14:02
  • @Nishant: I used this on iOS 6.0 and it is crashing as it could not fetch objectAtIndex:2. – parilogic May 29 '13 at 11:58
1

Whenever I need some open-source component, I go to cocoacontrols : maybe this one https://www.cocoacontrols.com/controls/ssswitch (I haven't tried...)

Vinzzz
  • 11,746
  • 5
  • 36
  • 42