ios 如何更改 UISwitch 默认颜色(蓝色)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8603535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:00:18  来源:igfitidea点击:

How to change the UISwitch default color(blue)

iosiphoneios5ios4

提问by Prasad

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

如何更改 UISwitch 的默认颜色(蓝色)?

回答by NicholasTGD

I think what you are looking for is something like this

我认为你正在寻找的是这样的

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

回答by jeddai

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

在 Xcode 5 和 iOS 7 中,它现在位于属性检查器中:

enter image description here

在此处输入图片说明

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

更改 On Tint 将更改按钮打开时的颜色。

enter image description here

在此处输入图片说明

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

我希望这就是你要找的!即使你在三年前发布了这个问题。

回答by Vyacheslav

Swift 3Swift 4

斯威夫特 3斯威夫特 4

workable solution

可行的解决方案

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

回答by WrightsCS

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

在 iOS 5 之前,如果不编写自己的自定义控件UISwitch,也许使用UISegmentedControl,Apple 不允许您更改标准UISwitch.

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

有一个私有属性setAlternateColor: YES会将颜色更改为橙​​色,您需要为UISwitch该类创建一个类别,但这不会在 Apple 过程中获得批准。

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

以下是一些UISwitch用于 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)
  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 (使用UISegmentedControl

Introduced in iOS 5, the UISwitchnow has an onTintColorproperty.

在 iOS 5 中引入,UISwitch现在有一个onTintColor属性。

[mySwitch setOnTintColor: [UIColor blackColor]];

回答by Tai Le

Swift 3:

斯威夫特 3:

yourSwitch.onTintColor = .red

回答by Fry

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

最后,在 iOS5 中,您可以使用 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 !

我希望这有帮助!

回答by Yongqiang Zhou

Set tint color for a specific UISwitch:

为特定的 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