28
 UIBarButtonItem *doneitem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)]autorelease];
    self.navigationItem.rightBarButtonItem=doneitem;

This is the code of my app, I need to add a image on this button ?

Please help me.

12 Answers 12

80

Try this code:

UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(sendmail)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];
Sign up to request clarification or add additional context in comments.

3 Comments

IT works great but when i click on the Button app is crash and simulator quit suddenly.
How can I do it and dont lose the Segue action defined to the Button in the storyboard?
Renato: Give the segue a name and call [self performSegueWithIdentifier:@"yourSegueName" sender:self]; in the button's target action.
54
 UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"XXXXXXX.png"] 
                                                  style:UIBarButtonItemStylePlain 
                                                  target:self 
                                                  action:@selector(yourMethod)];

self.navigationItem.rightBarButtonItem=_btn;

3 Comments

Just need measures Button
Totally agree with @onmyway133. Really simple and helpful answer.
true, this makes thing a lot easier.
6

Please try this code :

UIButton *btnNext1 =[[UIButton alloc] init];
[btnNext1 setBackgroundImage:[UIImage imageNamed:@"btnNext.png"] forState:UIControlStateNormal];

btnNext1.frame = CGRectMake(100, 100, 50, 30);
UIBarButtonItem *btnNext =[[UIBarButtonItem alloc] initWithCustomView:btnNext1];
[btnNext1 addTarget:self action:@selector(nextButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = btnNext;

Comments

5
UIButton *urButton = [UIButton buttonWithType:UIButtonTypeCustom];
urButton.frame = urRequiredFrame;
[urButton setImage:urImage forState:UIControlStateNormal];
[urButton addTarget:self action:@selector(donePressed:)
     forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithCustomView:urButton];
self.navigationItem.rightBarButtonItem=doneButton;

1 Comment

urRequiredFrame is the frame you want the button to be in and urImage is the the image u want as the image for the button
5

In case anyone needs Swift code for accepted answer:

let infoImage = UIImage(named: "my-icon-32.png")
let imgWidth = infoImage?.size.width
let imgHeight = infoImage?.size.height
let button:UIButton = UIButton(frame: CGRect(x: 0,y: 0,width: imgWidth!, height: imgHeight!))
button.setBackgroundImage(infoImage, forState: .Normal)
button.addTarget(self, action: Selector("openInfo"), forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)

P.S. Gurpreet Singh answer is working only for transparent PNG also I have to set button tintcolor other than clearColor.

Comments

4

In Swift

Other solution using withRenderingMode(UIImage.RenderingMode.alwaysOriginal) method below:

let img = UIImage(named: "picture")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItem.Style.plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = leftBarButtonItem

Comments

3

Here's what works for me. I found 30x30 to be a good size for the button in the nav bar. The UIImage scales to the button size automatically.

UIImage *image = [UIImage imageNamed:@"XXXXXXXXXX"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenHighlighted = NO;
UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightButton;

Comments

0
[doneItem setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];

Comments

0

One simple way :

UIImage* customImg = [UIImage imageNamed:@"custom-img.png"];

UIBarButtonItem *_customButton = [[UIBarButtonItem alloc] initWithImage:customImg style:UIBarButtonItemStyleDone target:nil action:nil];

self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:_customButton, nil];

Then if you need more than one button you can just and an other UIBarButtonItem to initWithObjects.

Comments

0

Do this. It's way more simple.

  1. Put the image file in your project directory

  2. Add the file into Xcode (Right click on the Xcode project, add files to "Project Name")

  3. Select your UIBarButtonItem in Storyboard

  4. Click on "Image" and find your image (See Screenshot)

  5. Celebrate, because it will work perfectly and it requires no unnecessary code.

enter image description here

3 Comments

I did this and my image is showing a flat blue rectangle not the image I selected from my project. Any ideas?
To answer my own comment, I did not have alpha correctly set up in the .png image file, I had to modify it with GIMP to set white color to alpha.
how do you provide two separate images this way for selected/unselected states
0

To pick up on the answer by Gurpreet Singh. To keep the current status of an already existing button re-use the target and action.

SEL selector = self.navigationItem.rightBarButtonItem.action;
id target = self.navigationItem.rightBarButtonItem.target;
UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_close.png"]
                                                      style:UIBarButtonItemStylePlain
                                                     target:target
                                                     action:selector];

self.navigationItem.rightBarButtonItem = _btn;

Comments

0
   let sliderImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 30))
    sliderImageView.contentMode = .ScaleAspectFit
    sliderImageView.image = UIImage(named: "sliderMenu")

    OR

    var sliderImage = UIImage(named: "sliderMenu")
    navigationItem.leftBarButtonItem?.image = sliderImage

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.