I'm new to iOS, while i'm trying to add on/off images to UISwitch in the UIStoryboard, it's not working. It is deprecated in iOS 10. I tried through code also but it's not working.
elseSwitch.onImage = UIImage(named: "switchOff")
elseSwitch.offImage = UIImage(named: "switchOff")
onImage
and offImage
has no effect on UISwitch
anymore as you've discovered :)
Instead you can use
onTintColor
to set the tint color of the switch when it is turned on.tintColor
to set the tint color of the switch when it is turned off.thumbTintColor
to set the tint color of the thumb.You can read more about it here
Here is an example using those three properties:
@IBOutlet weak var toggleSwitch: UISwitch! {
didSet {
toggleSwitch.isOn = true
toggleSwitch.tintColor = UIColor.red
toggleSwitch.onTintColor = UIColor.blue
toggleSwitch.thumbTintColor = UIColor.brown
}
}
Which gives me this beautiful switch when turned off
And this when turned on
(I'm a developer not a designer if you can't tell ;))
So in your case you could use some shade of grey for the onTintColor
and tintColor
to get you to this result
Hope that helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With