UISwitch on / off not working iOS10

I am new to iOS, while I try to add / disable images in UISwitch in UIStoryboard, it does not work. It is deprecated in iOS 10. I also tried the code, but it does not work.

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

He does not work. [! [enter image description here] [1]] [1]

+4
source share
1 answer

onImage and offImage no longer affect UISwitch , as you discovered :)

You can use instead

  • onTintColor to set the hue color of the switch when it is on.
  • tintColor to set the hue color of the switch when it is off.
  • thumbTintColor to set thumb tint color.

You can find out more about this.

Here is an example of using these three properties:

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

What gives me this beautiful switch when turned off

off switch

And this is when you turn it on

on switch

(I'm a developer, not a designer, if you cannot say;))

So, in your case, you can use the grayscale for onTintColor and tintColor to get this result

enter image description here

enter image description here

Hope this helps.

+9
source

Source: https://habr.com/ru/post/1260273/


All Articles