Creating a custom toggle with UIButton in Interface Builder

Go To


Is this possible? It seems shoddy for it not to be.

I'm trying to create a toggle button (not a UISwitch) in IB. For example a mute button, when muted would have some indication that sound is disabled, when you hit it, that indicator disappears (but still the same underlying graphic), and toggles between these states each time it is pressed.

This functionality can be achieved using the selected property, however you can't change the Selected AND Highlighted property in IB like you can in code, so whenever the button is pressed, no matter the state, the highlighted image is the same, which looks terrible and glitchy.

Is there a way to fix this with just IB, or do I have to make a custom class to avoid manually loading in all these buttons?

2012-04-03 19:35
by user1137704


Have you thought about subclassing UIButton to add in the things you need?

2012-04-03 20:50
by LJ Wilson
It's possible to do it with a regular UIButton in code; yes, I have thought about subclassing it and just added some code to autodetect and set the Selected/Highlighted state upon initialization, but it would be nicer if I didn't have to do this. If I do have to go this route however, I'm not sure what to override, would it just be -initWithFrame: - user1137704 2012-04-03 21:09
For example, you can do setImage:@"some image" forState:(UIControlStateHighlighted | UIControlStateSelected), which would solve the problem, but there doesn't seem to be an equivalent in IB. The only problem with subclassing, is recognizing what the value should be - user1137704 2012-04-03 21:16