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
How to change the UISwitch default color(blue)
提问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 中,它现在位于属性检查器中:
Changing the On Tint will change the button's color when it's turned on.
更改 On Tint 将更改按钮打开时的颜色。
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 UISwitch
control, 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: 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.
有一个私有属性setAlternateColor: YES
会将颜色更改为橙色,您需要为UISwitch
该类创建一个类别,但这不会在 Apple 过程中获得批准。
Here are a few custom UISwitch
projects for use in iOS 3.0 - 4.1:
以下是一些UISwitch
用于 iOS 3.0 - 4.1 的自定义项目:
- http://osiris.laya.com/projects/rcswitch/
- http://www.alexcurylo.com/blog/2010/07/30/custom-uiswitch/
- StackOverflow Anser: https://stackoverflow.com/a/5088099/171206(using
UISegmentedControl
)
- http://osiris.laya.com/projects/rcswitch/
- http://www.alexcurylo.com/blog/2010/07/30/custom-uiswitch/
- StackOverflow Anser:https://stackoverflow.com/a/5088099/171206 (使用
UISegmentedControl
)
Introduced in iOS 5, the UISwitch
now has an onTintColor
property.
在 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
生产这个
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