UISwitch开/关图像不工作iOS10

问题描述:

我是新来的iOS,而我试图添加/关闭图像到UIStoryboard UISwitch,它不工作。它在iOS 10中已被弃用。我也尝试了代码,但它不起作用。UISwitch开/关图像不工作iOS10

elseSwitch.onImage = UIImage(named: "switchOff") 
elseSwitch.offImage = UIImage(named: "switchOff") 

它不工作。[![在这里输入的形象描述] [1] [1]

+0

我有一个代码,但在目标c不在迅速 –

+1

可能重复[为什么UISwitch onImage/offImage属性不适用于iOS 7?](http://*.com/questions/22843747/why-uiswitch- onimage-offimage-properties-are-not-working-on-ios-7) –

+1

[Custom On/Off Image iOS 7.0 UI Switch]可能的重复(http://*.com/questions/19410300/custom-on- off-image-ios-7-0-ui-switch) –

onImageoffImageUISwitch没有作用了,因为你已经发现了:)

相反,您可以使用

  • onTintColor设置开关打开时的色调颜色。
  • tintColor设置开关关闭时的色调颜色。
  • thumbTintColor设置拇指的色调颜色。

你可以阅读更多关于它here

下面是一个使用这三个属性的示例:当关闭

off switch

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

这给了我这个美丽的开关

而这个打开的时候

on switch

(我是一个开发商不是一个设计师,如果你不能告诉;))

所以你的情况,你可以使用一些灰色灯罩为onTintColortintColor让你这个结果

enter image description here

enter image description here

希望帮助你。