UISwitch set on/off Image

后端 未结 3 1443
無奈伤痛
無奈伤痛 2021-01-04 11:37

I want to set my Switch like this:

But I try in ios9 , it does not work. I saw in apple UISwitch Class Reference. It says that :

Discussion In iOS 7

3条回答
  •  一个人的身影
    2021-01-04 12:13

    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:

    Then the final result is:

提交回复
热议问题