Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of UISwitch appwise

I am using UISwitch in iOS 3 to make a switch element in my app. It has default color set to blue, but I want to change its color to brown.

How can I choose a different color for the UISwitch element in iOS 3?

How can I choose a different color for the UISwitch element in a modern iOS app (iOS 5+)?

like image 209
Ankit Sachan Avatar asked Nov 12 '09 06:11

Ankit Sachan


4 Answers

Finally, with iOS 5 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];

produces this:

Enter image description here

like image 109
Fry Avatar answered Nov 15 '22 08:11

Fry


For a global change for all UISwitch elements in Swift 3, use the appearance proxy:

UISwitch.appearance().onTintColor = UIColor.brown

under the AppDelegate application:didFinishLaunchingWithOptions: method.

like image 27
headuck Avatar answered Nov 15 '22 08:11

headuck


Currently you are limited to text values of On/Off or 0/1 for a UISwitch. You can customize the color by using tint. For further customization I would suggest something like what's been posted above going with a completely custom solution

ex. [mySwitch setOnTintColor:[UIColor colorWithRed:0 green:175.0/255.0 blue:176.0/255.0 alpha:1.0]];

source: http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

EDIT: For iOS3, you are limited to a custom implimentation, I would suggest swapping two buttons with custom images. In later iOS versions you are given much more control and built in customization options.

like image 2
propstm Avatar answered Nov 15 '22 08:11

propstm


Take a look at the custom UISwitch control that I built to allow me to change the background color of the control. You could use the same method to change the text, the font, or the text color very easily.

http://www.homick.com/posts/custom-uiswitch-control

The code is available on GitHub and includes a PSD that is used to build three different PNG files that the control uses. You can modify the contents of the psd to recreate the PNG files in whatever format you like. Swap those into the control and away you go.

This gives a lot more options than just orange and blue.

like image 1
Duane Homick Avatar answered Nov 15 '22 08:11

Duane Homick