Swift - 如何更改 UISwitch 控件上曲线的颜色
Swift -How to change the color of the curve on a UISwitch control
如何更改 UISwitch
控件后面的曲线颜色?我尝试更改 borderColor 但它与框架有关。
这是另一个问题的示例照片。我想切换这张照片中银色曲线的颜色(不是圆形按钮,也不是橙色背景)
我尝试了这两个属性,但是 .layer.borderWidth = 2.0
和 .layer.cornerRadius = 15
改变了框架 而不是曲线
当开启时,我希望曲线为.clear
,当关闭时,我希望曲线为.red
lazy var switchControl: UISwitch = {
let switchControl = UISwitch()
switchControl.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
}()
@objc func switchValueDidChange(_ sender: UISwitch) {
if (sender.isOn == true) {
sender.layer.borderColor = UIColor.clear.cgColor // this doesn't work
} else {
sender.layer.borderColor = UIColor.red.cgColor // this doesn't work
}
}
更改 UISwitch 的 Tint 颜色
mSwitch.tintColor = offColor// 自定义颜色
我相信如果没有 SDK 的一些黑客技巧,这是不可能的。但我建议使用图像创建您自己的此类控件。
是的,正如 iChirag 所说,目前使用内置 UISwitch
.
无法做到这一点
至于为什么不可能,很难说。我的猜测是,这是因为 Apple 鼓励在 Human Interface Guidelines 中使用有限的调色板。在 iOS 应用程序中使用颜色的主要目的基本上是传达信息,帮助用户一眼区分 UI 的不同部分或引起对用户操作的注意。也许他们认为为 UISwitch
的曲线设置自定义颜色超出了这些目的。
话虽如此,如果您需要这种外观,构建您自己的自定义 UISwitch
相当容易。这是一个入门教程,希望对您有所帮助:
如何更改 UISwitch
控件后面的曲线颜色?我尝试更改 borderColor 但它与框架有关。
这是另一个问题的示例照片。我想切换这张照片中银色曲线的颜色(不是圆形按钮,也不是橙色背景)
我尝试了这两个属性,但是 .layer.borderWidth = 2.0
和 .layer.cornerRadius = 15
改变了框架 而不是曲线
当开启时,我希望曲线为.clear
,当关闭时,我希望曲线为.red
lazy var switchControl: UISwitch = {
let switchControl = UISwitch()
switchControl.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
}()
@objc func switchValueDidChange(_ sender: UISwitch) {
if (sender.isOn == true) {
sender.layer.borderColor = UIColor.clear.cgColor // this doesn't work
} else {
sender.layer.borderColor = UIColor.red.cgColor // this doesn't work
}
}
更改 UISwitch 的 Tint 颜色
mSwitch.tintColor = offColor// 自定义颜色
我相信如果没有 SDK 的一些黑客技巧,这是不可能的。但我建议使用图像创建您自己的此类控件。
是的,正如 iChirag 所说,目前使用内置 UISwitch
.
至于为什么不可能,很难说。我的猜测是,这是因为 Apple 鼓励在 Human Interface Guidelines 中使用有限的调色板。在 iOS 应用程序中使用颜色的主要目的基本上是传达信息,帮助用户一眼区分 UI 的不同部分或引起对用户操作的注意。也许他们认为为 UISwitch
的曲线设置自定义颜色超出了这些目的。
话虽如此,如果您需要这种外观,构建您自己的自定义 UISwitch
相当容易。这是一个入门教程,希望对您有所帮助: