UISwitch 打开/关闭图像不工作 iOS 10

UISwitch on / off image not working iOS 10

我是 iOS 的新手,当我尝试将 on/off 图像添加到 UIStoryboard 中的 UISwitch 时,它不起作用。它在 iOS 10 中被弃用。我也尝试通过代码但它不起作用。

elseSwitch.onImage = UIImage(named: "switchOff")
elseSwitch.offImage = UIImage(named: "switchOff")
正如您所发现的,

onImageoffImageUISwitch 没有任何影响:)

您可以使用

  • onTintColor设置开关打开时的色调。
  • tintColor 设置开关关闭时的色调。
  • thumbTintColor 设置缩略图的色调。

您可以阅读更多相关信息here

下面是使用这三个属性的示例:

@IBOutlet weak var toggleSwitch: UISwitch! {
    didSet {
        toggleSwitch.isOn = true
        toggleSwitch.tintColor = UIColor.red
        toggleSwitch.onTintColor = UIColor.blue
        toggleSwitch.thumbTintColor = UIColor.brown
    }
}

它在关闭时给了我这个漂亮的开关

打开时会这样

(如果你分不清我是开发者而不是设计师 ;))

因此,在您的情况下,您可以为 onTintColortintColor 使用一些灰色阴影来获得此结果

希望对你有所帮助。