How to change the UISwitch default color(blue)

IosIphoneIos5Ios4

Ios Problem Overview


How to change the default color(blue) of a UISwitch?

Ios Solutions


Solution 1 - Ios

I think what you are looking for is something like this

UISwitch *testSwitch; //just something I made up
[testSwitch setOnTintColor:[UIColor greenColor]];

Solution 2 - Ios

In Xcode 5 and iOS 7 it's now in the attributes inspector:

enter image description here

Changing the On Tint will change the button's color when it's turned on.

enter image description here

I hope that's what you were looking for! Even though you posted that question like three years ago.

Solution 3 - Ios

Swift 3 Swift 4

workable solution

var switcher = UISwitch()
switcher.onTintColor = .green
switcher.tintColor = .green

Solution 4 - Ios

Prior to iOS 5, without writing your own custom UISwitch control, perhaps using a UISegmentedControl, Apple did not allow you to change the color of a standard UISwitch.

There is a private property setAlternateColor: YES which will change the color to orange, and you would need to create a category for the UISwitch class, but this will not be approved in the Apple review process.

Here are a few custom UISwitch projects for use in iOS 3.0 - 4.1:

  1. http://osiris.laya.com/projects/rcswitch/
  2. http://www.alexcurylo.com/blog/2010/07/30/custom-uiswitch/
  3. StackOverflow Anser: https://stackoverflow.com/a/5088099/171206 (using UISegmentedControl)

Introduced in iOS 5, the UISwitch now has an onTintColor property.

[mySwitch setOnTintColor: [UIColor blackColor]];

Solution 5 - Ios

Swift 3:

yourSwitch.onTintColor = .red

Solution 6 - Ios

Set tint color for a specific UISwitch:

var switcher = UISwitch()
switcher.onTintColor = .red
switcher.tintColor = .red

Set tint color for your app:

let switchApperence = UISwitch.appearance()
switchApperence.tintColor = .red
switchApperence.onTintColor = .red

Solution 7 - Ios

Finally, with iOS5 you can change the color of the switch with the property onTintColor.

UISwitch *s = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
s.on = YES;
s.onTintColor = [UIColor redColor];
[self.view addSubview:s];
[s release];

produce this

enter image description here

I hope this help !

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrasadView Question on Stackoverflow
Solution 1 - IosNicholasTGDView Answer on Stackoverflow
Solution 2 - IosjeddaiView Answer on Stackoverflow
Solution 3 - IosVyacheslavView Answer on Stackoverflow
Solution 4 - IosWrightsCSView Answer on Stackoverflow
Solution 5 - IosTai LeView Answer on Stackoverflow
Solution 6 - IosYongqiang ZhouView Answer on Stackoverflow
Solution 7 - IosFryView Answer on Stackoverflow