iPhone toggle button implementation

iphone

Solution

You can use `UIButton` and set its `enabled` property.

In Interface Builder, you can set separate images for the enabled and disabled states, and the `IBAction` change the property.

Problem

I would like to create a toggle button in my iPhone application. However, I don't know exactly what would be the best approach for this. I am considering two options. I could subclass an UIButton. That way I do not have to implement touch handling. I could just create a method that returns a boolean indicating whether the button is in on or off state. I could also start with an UIView subclass and create it from scratch. I suppose it would be easier to do things like a transition animation, but I would have to create my own touch handling. What do you think is the best one? Or would you know a better way? (Maybe there is an open-source/free view available on the web?) EDIT: I don't want to use the UISwitch because I want my toggle button to look like that. I want it to be a big rectangular button and make the background change to indicate the state. Thanks!

Original source