Updating UIImage on UiBarButtonItem from identifier to image

Related searches

I am currently making an application for IOS but I am having trouble changing an image... what a shame...

The situation is like this : I have a toolbar containing my items, and one of this items is the play button created from the identifier "Play". So I have my Play button without problem. Now I just want to change it to a pause image when I click on item and then switch again etc. So I liked in my .h the item giving me :

@property (weak, nonatomic) IBOutlet UIBarButtonItem *play;

I tried many answers I found on this website and none of them did work on my case :/ The last one I tried was something like this :

UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[button1 addTarget:self action:@selector(showLeft:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
self.play = button;

I also tried :

self.play.customView = button1;

But none of them really work, I can get the image by doing

self.view = button1;

But that only the picture (so the creation of the UIImage is okay) in the middle of the screen so....

(If you can also tell me how to go back to the play using identifier it would also be very helpful thanks a lot)

Thanks for your help.

I think this should work.

-(IBAction)buttonClick:(UIBarButtonItem *)sender {

    if ([[sender backgroundImageForState:UIControlStateNormal barMetrics:UIBarMetricsDefault] isEqual:[UIImage imageNamed:@"Play.jpg"]]) {
        [sender setBackgroundImage:[UIImage imageNamed:@"Pause.jpg"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    }else{
        [sender setBackgroundImage:[UIImage imageNamed:@"Play.jpg"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    }
}

Of course, you'll have to set the background image initially (in viewDidLoad) to the "Play" image for this to work.

After Edit:

If you want to use the system play and pause button, as far as I know, you have to replace the button. I don't think there's a way to just change the image. So, I did it this way. The button was set up in IB with the outlet playPauseButton, and the action playClick. I also made an outlet to the tool bar (toolBar).

-(IBAction)playClick:(UIBarButtonItem *)sender {
    UIBarButtonItem *pause = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(pauseClick:)];
    NSMutableArray *tbItems = [self.toolBar.items mutableCopy];
    [tbItems removeObject:self.playPauseButton];
    self.playPauseButton = pause;
    [tbItems addObject:pause];
    self.toolBar.items = tbItems;
}

-(void)pauseClick:(UIBarButtonItem *)sender {
    UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(playClick:)];
    NSMutableArray *tbItems = [self.toolBar.items mutableCopy];
    [tbItems removeObject:self.playPauseButton];
    self.playPauseButton = play;
    [tbItems addObject:play];
    self.toolBar.items = tbItems;
}

Updating UIImage on UiBarButtonItem from identifier to image, Updating UIImage on UiBarButtonItem from identifier to image - ios. my items, and one of this items is the play button created from the identifier "Play". Tag: ios,swift,uiimage. When a bar button (Play) belonging to the toolbar is pressed I want to change its icon to a Pause image. The Play image is set when button is first created under the Attribute Inspector for the Bar Button Identifier. The pause image I am trying to change the image too is also from the drop list under identifier .

If the button is already created and you have an outlet for it, you can simply set it like this:

UIImage* backgroundImage = [UIImage yourImage];
[self.barButtonItem setImage:backgroundImage];

Works like charm.

Change Image of UIBarButtonItem, That will give you the default Refresh icon. enter image description here. Add an IB Action. Control drag from the UIBarButtonItem to the View Controller to add an � UIBarButtonItem Image and Text I want to add an image and text to a BarButtonItem but can’t in IB. I looked into EdgeInsets but couldn’t get them to work with the image.

Create a custom button. Change the size of the button to match the image. Set image of the button. Add button view to UIBarButton play.

.h

IBOutlet UIBarButtonItem *play;

@property(nonatomic, retain) IBOutlet UIBarButtonItem *play;

.m

UIImage *image = [UIImage imageNamed:@"image.png"];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button1 setImage:image forState:UIControlStateNormal];
play = [[UIBarButtonItem alloc] initWithCustomView:button1];

Make sure you connect your outlet for the UIBarButtonItem in Interface Builder.

Good luck!

UIBarButtonItem, Create UIBarButtonItem programmatically; Create UIImage programmatically; Add iOS 11: Change size of UIBarButtonItem (image) in Swift 3. gistfile1.txt. func setUpMenuButton(){. let menuBtn = UIButton(type: .custom). Update 20120506. Uibarbuttonitem image template � Get sharepoint site id � How to prevent table� Overview. You typically use Interface Builder to create and configure bar button items. However, you can customize the appearance of buttons by sending the setter messages to UIBar Button Item Appearance to customize all buttons, or to a specific UIBar Button Item instance.

The answer of rdelmar it's great. I would like to add an enhancement that avoid a flickering of the button because the remove and add of the button.

-(void)pauseClick:(UIBarButtonItem *)sender {
    UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(playClick:)];
    NSMutableArray *tbItems = [self.toolBar.items mutableCopy];
    self.playPauseButton = play;
    //instead of remove and add the new button, use the replaceObjectAtIndex method.
    [tbItems replaceObjectAtIndex:2 withObject:play];
    self.toolBar.items = tbItems;
}

UIBarButtonItem image stays blue and not the original color of the , This is how I would modify your original code: UIImageView *SOCImageView = [[ UIImageView alloc] initWithImage: [UIImage imageNamed:@"cancel_wide.png"]]; � Questions: I am trying to set an Image for bar button Item for that I have an image like: with resolution 30 * 30 but while I assign this Image to Bar button Its looks like: I have assigned image this way : and If I try this way like making an IBOutlet for the

You can try something like this for your custom button as one of the UIBarButtonItem.

 - (void)viewDidLoad
 {
   // add our custom image button as the nav bar's custom right view
   UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage    imageNamed:@"email.png"]
                                style:UIBarButtonItemStyleBordered target:self  action:@selector(action:)];
   self.navigationItem.rightBarButtonItem = addButton;
   [addButton release];
 }

- (IBAction)action:(id)sender
{
    // the custom icon button was clicked, handle it here
    // change image here

}

UIBarButtonItem: target-action not working?, ios documentation: Convert UIImage to/from base64 encoding. Example. Encoding //convert the image to NSData first let imageData:NSData = UIImagePNGRepresentation(image)!

This code example demonstrates how to: Create UIBarButtonItem programmatically Create UIImage programmatically Add image to UIBarButtonItem Add target action to UIBarButtonItem to call a local function import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Image needs to be added to project. let buttonIcon = UIImage(named: "left_side") let

UIBarButtonItem(UIImage, UIBarButtonItemStyle, EventHandler) Constructor that allows a custom image, style and evnet handler to be specied when the button is created. UIBarButtonItem(UIImage, UIBarButtonItemStyle, NSObject, Selector) Constructor that allows an image and style to be specified, along with a target-action pair.

Discussion. This modifier applies only to navigation bar back buttons and is ignored by other buttons. For good results, background Image must be a stretchable image.

Comments
  • Have you tried just changing the button's customView property to a view with the pause image?
  • Well I suggest to use images based on the different states. Set one image for UIControlStateNormal, and assign it play button and set another image for UIControlStateSelected and assign the pause button. Whenever the button is clicked toggle the state of button using a simlple rule like this; button.selected = !button.selected I hope this solves your problem.
  • My actual problem is that I wanted to use the play from ios by using the identifier "play" in xcode interface. We are getting closer because I can change the backgroundImage with your method but why doesn't it work in the same way when I do the same but changing the picture? There are no way to update directly the image ?
  • I just checked for identifier changing but same results. creating : [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(pushPlay:)]; and same for the play one then in my code setting self.play = pauseIco but it does nothing.
  • I finally succed to make it works by putting only my own images, so without using identifier, but if you know a way to do it with the identifier I would be grateful to you. Thanks a lot.
  • @CladClad I updated my answer to show how I did it using the system buttons.
  • Finally I used 2 images and no identifiers for the moment but thanks for your help :)
  • I can't used this because my item in not in the navigation bar but in a toolbar in the middle of the screen.