Change color of UISwitch appwise

谁说我不能喝 提交于 2019-12-03 10:08:50
dlamblin

For a modern iOS XX version skip the answer as it has zero relevance lately. Still here? In iOS 3. You can't. But you know… people are landing here because of rank, and comments containing other versions and stuff so…

The fifth iOS release now allows a documented way of doing this: 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

Edit: I have no idea why someone would come through and roll back the answer to limit it to the question only while not doing the same to all the answers.

Edit more: In iOS 3 you had to use some undocumented feature. There used to be a link about that here but it broke. And if you did that your app might get rejected because of changing the color.

It kind of starts with this, which should be enough for you to find it somewhere else:

UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:someRectangle];
[aSwitch setAlternateColors:YES];
//Do something with aSwitch
[aSwitch release];

I can't recommend it. And that turns out unpopular.

Fry

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:

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.

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.

Duane Homick

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.

Kevin ABRIOUX

For Swift 3:

var mySwitch : UISwitch = ...
mySwitch.onTintColor = UIColor.blue
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!