How to use a UIButton as a toggle switch

前端 未结 11 765
我在风中等你
我在风中等你 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 19:10

    UIButton does supports a "toggle" functionality by default. To use this you need to set a different image or even text color in Interface Builder for the State Configuration = Selected, and use the selected property of UIButton to toggle its state.

    Code:

    - (IBAction)yourButtonTouch:(UIButton *)sender {
    
        sender.selected = !sender.selected;
        if (sender.selected) {
            //...
            // Action to be performed when button is selected or 
            // the first touch
            // ... 
    
        }
    }
    

提交回复
热议问题