54

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

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
Prasad
  • 1,904
  • 4
  • 19
  • 24
  • 1
    nowadays (2019) it's just the badly-named `.onTintColor` . Note that this has no relation to the usual "tintColor". – Fattie Nov 21 '19 at 18:30

7 Answers7

71

I think what you are looking for is something like this

UISwitch *testSwitch; //just something I made up
[testSwitch setOnTintColor:[UIColor greenColor]];
Nazik
  • 8,696
  • 27
  • 77
  • 123
NicholasTGD
  • 1,256
  • 1
  • 10
  • 11
50

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.

jeddai
  • 799
  • 9
  • 21
25

Swift 3 Swift 4

workable solution

var switcher = UISwitch()
switcher.onTintColor = .green
switcher.tintColor = .green
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
8

Swift 3:

yourSwitch.onTintColor = .red
Tai Le
  • 8,530
  • 5
  • 41
  • 34
8

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]];
Community
  • 1
  • 1
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
3

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
Yongqiang Zhou
  • 322
  • 1
  • 12
2

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 !

Fry
  • 6,235
  • 8
  • 54
  • 93