How to use a UIButton as a toggle switch

前端 未结 11 762
我在风中等你
我在风中等你 2020-12-02 18:44

I am using a UIButton of custom type and what I want is use it like a toggle switch with the change of image. Like when it is clicked if previously it was not in selected mo

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 18:59

    My implementation of a UIButton as a Switch.

    
    class ButtonSwitch: UIButton {
      override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
        if allControlEvents == .touchUpInside {
          isSelected.toggle()
        }
        super.sendAction(action, to: target, for: event)
      }
    }
    

提交回复
热议问题