In IB I can set the identifier of a UIBarButtonItem to 'play' which adds an image of a play button (right-pointing triangle).
Is there a way to change this image programmatically? I want to change it to 'pause' when the play button is pressed.
I know you can initialize a UIBarButtonItem with an identifier but I've yet to find a way to change it after it's been initialized. Is this even possible?
The only thing I can think of is to remove the old button and initialize a new one in its place, but this hardly seems efficient.
Any thoughts?
Best Answer
Ok I've googled this question to death and ran into sample code from Apple where they do exactly the same thing (toggle play/pause button graphic on a toolbar button). But instead of using the built in play and pause identifiers of
UIBarButtonItem
they use a customUIButton
and toggle custom images.So if Apple goes through the trouble of creating and toggling custom images on a UIButton instead of the built in play and pause
UIBarButtonItem
buttons then I think it's pretty safe to say there's no way to programatically change the identifier of aUIBarButtonItem
.This is what they (Apple) do to toggle the images when the button is pressed:
Replace p.playing with whatever BOOL you want to hold the state of your button.
playButton
is the customUIButton
in the toolbar.pauseBtnBG
andplayBtnBG
are the images to toggle.