Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISwitch on / off image not working iOS 10

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")
like image 615
Joe Avatar asked Nov 24 '16 11:11

Joe


1 Answers

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

off switch

And this when turned on

on switch

(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

enter image description here

enter image description here

Hope that helps you.

like image 169
pbodsk Avatar answered Oct 20 '22 08:10

pbodsk