UISwitch set on/off Image

后端 未结 3 1411
無奈伤痛
無奈伤痛 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:28

    Not an exact answer to your question, but if you want a completely custom button switch programmatically (that you can add text to), this will work too:

     import UIKit
    
     class RDHiddenVisibleButton: UIButton {
    
        // Hidden / Visible Button Function
        var isOn = false
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            initButton()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            initButton()
        }
    
        func initButton() {
            layer.borderWidth = 2.0
            layer.borderColor = Colors.radiusGreen.cgColor
            layer.cornerRadius = frame.size.height/2
    
            setTitleColor(Colors.radiusGreen, for: .normal)
            addTarget(self, action: #selector(RDHiddenVisibleButton.buttonPressed), for: .touchUpInside)
    
        }
    
    @objc func buttonPressed() {
            activateButton(bool: !isOn)
        }
    
        func activateButton(bool: Bool) {
    
            isOn = bool
    
            let color = bool ? Colors.radiusGreen : .clear
            let title = bool ? "Hidden" : "Visible"
            let titleColor = bool ? . white : Colors.radiusGreen
    
            setTitle(title, for: .normal)
            setTitleColor(titleColor, for: .normal)
            backgroundColor = color
        }
    }
    

提交回复
热议问题