【发布时间】:2012-01-26 01:47:16
【问题描述】:
如何更改 UISwitch 的默认颜色(蓝色)?
【问题讨论】:
-
现在(2019 年)只是名字不好的
.onTintColor。请注意,这与通常的“tintColor”无关。
如何更改 UISwitch 的默认颜色(蓝色)?
【问题讨论】:
.onTintColor 。请注意,这与通常的“tintColor”无关。
我认为您正在寻找的是这样的东西
UISwitch *testSwitch; //just something I made up
[testSwitch setOnTintColor:[UIColor greenColor]];
【讨论】:
在 iOS 5 之前,无需编写您自己的自定义 UISwitch 控件,可能使用 UISegmentedControl,Apple 不允许您更改标准 UISwitch 的颜色。
有一个私有属性 setAlternateColor: YES 会将颜色更改为橙色,您需要为 UISwitch 类创建一个类别,但这不会在 Apple 中获得批准审查过程。
以下是一些用于 iOS 3.0 - 4.1 的自定义 UISwitch 项目:
UISegmentedControl)在 iOS 5 中引入的UISwitch 现在有一个onTintColor 属性。
[mySwitch setOnTintColor: [UIColor blackColor]];
【讨论】:
最后,在 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];
制作这个
希望对你有所帮助!
【讨论】:
在 Xcode 5 和 iOS 7 中,它现在位于属性检查器中:
更改 On Tint 将更改按钮打开时的颜色。
我希望这就是您想要的!即使您在三年前发布了这个问题。
【讨论】:
斯威夫特 3:
yourSwitch.onTintColor = .red
【讨论】:
斯威夫特 3 斯威夫特 4
可行的解决方案
var switcher = UISwitch()
switcher.onTintColor = .green
switcher.tintColor = .green
【讨论】:
为特定的 UISwitch 设置 tint color:
var switcher = UISwitch()
switcher.onTintColor = .red
switcher.tintColor = .red
为您的应用设置色调:
let switchApperence = UISwitch.appearance()
switchApperence.tintColor = .red
switchApperence.onTintColor = .red
【讨论】: