UISwitch set on/off Image

For iOS 13, you could do this way:

   let switcher = UISwitch()
    switcher.addTarget(self, action: #selector(pressed), for: .valueChanged)
    

 @objc func pressed(sender: UISwitch) {
        let color = UIColor(patternImage: UIImage(named: sender.isOn ? "on.png": "off.png")!)
        if sender.isOn {
            sender.onTintColor = color
        } else {
            sender.tintColor = color
            sender.subviews[0].subviews[0].backgroundColor = color
        }
    }

NOTE: your image should look like:

enter image description here

Then the final result is:

enter image description here


Use a UIButton instead.

let switchButton = UIButton(type: .Custom)
switchButton.selected = true
switchButton.setImage(UIImage(named: "on-switch"), forState: .Selected)
switchButton.setImage(UIImage(named: "off-switch"), forState: .Normal)

Use switchButton.isSelected instead of switch1.on. You'll have to toggle switchButton.isSelected when it is tapped, which you can do like this:

switchButton.isSelected.toggle()