I am using UIBarButtonSystemItemPlay to play my audio file and i want to change it's style dynamically when i click it. Is it possible?, if yes, then please help me. This is my code, where _playPause is IBOutlet of UIBarButtonSystemItemPlay. Thanks in advance.
- (IBAction)playPause:(UIBarButtonItem *) sender
{
if (_playPause.style == UIBarButtonSystemItemPlay)
{
[_playPause setStyle:UIBarButtonSystemItemPause];
[audio play];
}
else
{
[_playPause setStyle:UIBarButtonSystemItemPlay];
[audio pause];
}
}
UIBarButtonSystemItemPlay
or UIBarButtonSystemItemPause
cannot be retrieved by style property. style is of type UIBarButtonItemStyle
.
Check the documentation here.
I suggest to create two different UIBarButtonItem
and then enable or disable (for example) them alternately. It is also possible to remove the current visible button and add a new one with a different UIBarButtonSystemItem
.
Here's a pretty complete solution for this that I just implemented:
First, define vars to hold both buttons:
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *playButton;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *pauseButton;
(My understanding here is that the toolbar is a weak ref since it is being defined via IB and it has a strong pointer to it, but that the play/pause buttons are strong pointers because we are creating them below. But my fu is a little weak on this front as a fairly newbie, so corrections appreciated.)
Second, in IB create just a play button (no pause button) and tie it to the above playButton var.
Third, set up this method:
- (void) setAsPlaying:(BOOL)isPlaying
{
self.rootViewController.playing = isPlaying;
// we need to change which of play/pause buttons are showing, if the one to
// reverse current action isn't showing
if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
{
UIBarButtonItem *buttonToRemove = nil;
UIBarButtonItem *buttonToAdd = nil;
if (isPlaying)
{
buttonToRemove = self.playButton;
self.playButton = nil;
self.pauseButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause
target:self
action:@selector(pauseAudio:)];
buttonToAdd = self.pauseButton;
}
else
{
buttonToRemove = self.pauseButton;
self.pauseButton = nil;
self.playButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:@selector(playAudio:)];
buttonToAdd = self.playButton;
}
// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];
// Remove a button from the toolbar and add the other one
if (buttonToRemove)
[toolbarButtons removeObject:buttonToRemove];
if (![toolbarButtons containsObject:buttonToAdd])
[toolbarButtons insertObject:buttonToAdd atIndex:2]; // vary this index to put in diff spots in toolbar
[self.toolbar setItems:toolbarButtons];
}
}
Fourth, call setAsPlaying wherever you are going to play or pause mode. For instance:
-(IBAction) playAudio:(id)sender
{
[self.audioPlayer play];
[self setAsPlaying:YES];
}
-(IBAction) pauseAudio:(id)sender
{
[self.audioPlayer pause];
[self setAsPlaying:NO];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With