How can I change the style of a UIBarButtonItem in code

I am using UIBarButtonSystemItemPlay to play my audio file and I want to dynamically change its style when I click it. Is this possible? If yes, then please help me. This is my code where _playPause is the IBOutlet UIBarButtonSystemItemPlay. Thanks in advance.

- (IBAction)playPause:(UIBarButtonItem *) sender { if (_playPause.style == UIBarButtonSystemItemPlay) { [_playPause setStyle:UIBarButtonSystemItemPause]; [audio play]; } else { [_playPause setStyle:UIBarButtonSystemItemPlay]; [audio pause]; } } 
+8
ios objective-c iphone
source share
4 answers

UIBarButtonSystemItemPlay or UIBarButtonSystemItemPause cannot be restored using the style property. the style is of type UIBarButtonItemStyle .

Check out the documentation here .

I suggest creating two different UIBarButtonItem and then turning them on or off alternately (for example). You can also delete the current visible button and add a new one with another UIBarButtonSystemItem .

+5
source share

Here is a fairly complete solution for this that I just implemented:

Define vars first to hold both buttons:

 @property (weak, nonatomic) IBOutlet UIToolbar *toolbar; @property (strong, nonatomic) IBOutlet UIBarButtonItem *playButton; @property (strong, nonatomic) IBOutlet UIBarButtonItem *pauseButton; 

(I understand that the toolbar is a weak ref, because it is defined through IB, and it has a strong pointer to it, but the play / pause buttons are strong pointers, because we create them below. Fu is a little weak on this front as a pretty newbie , so the fixes were appreciated.)

Secondly, in IB, create a play button (without a pause button) and bind it to the previous version of playButton var.

Third, configure 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 in pause mode. For example:

 -(IBAction) playAudio:(id)sender { [self.audioPlayer play]; [self setAsPlaying:YES]; } -(IBAction) pauseAudio:(id)sender { [self.audioPlayer pause]; [self setAsPlaying:NO]; } 
+2
source share

I believe that as soon as the style is installed initially (i.e. in the interface builder or when creating a button in the code), you cannot change it.

The only other option is to remove the button in the code and create a new one in its place using a different style. You can call this method:

 - (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action 

For more on UIBarButtonItem see this .

0
source share

Yes, you cannot change the β€œstyle” (it is not called the actual style) after installing it.

Yes, you can make another button that fixes it.

Here is the reuse code that does this:

 UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:controller.navigationItem.rightBarButtonItem.target action:controller.navigationItem.rightBarButtonItem.action]; controller.navigationItem.rightBarButtonItem = newButton; 

This is usually a self controller.

0
source share

All Articles