M Faheem Rajput
M Faheem Rajput

Reputation: 778

How to add custom image in navigation bar button item?

 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.

Upvotes: 28

Views: 78541

Answers (12)

King-Wizard
King-Wizard

Reputation: 15694

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

Upvotes: 4

Alvin George
Alvin George

Reputation: 14296

   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

Upvotes: 0

waffles
waffles

Reputation: 1396

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;

Upvotes: 3

limfinity
limfinity

Reputation: 746

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;

Upvotes: 0

Michael
Michael

Reputation: 9824

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

Upvotes: 0

Aks
Aks

Reputation: 8301

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.

Upvotes: 5

lapin
lapin

Reputation: 2148

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.

Upvotes: 0

Gurpreet Singh
Gurpreet Singh

Reputation: 1179

 UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"XXXXXXX.png"] 
                                                  style:UIBarButtonItemStylePlain 
                                                  target:self 
                                                  action:@selector(yourMethod)];

self.navigationItem.rightBarButtonItem=_btn;

Upvotes: 54

Mitesh Khatri
Mitesh Khatri

Reputation: 3955

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;

Upvotes: 6

visakh7
visakh7

Reputation: 26390

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;

Upvotes: 5

Praveen S
Praveen S

Reputation: 10395

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

Upvotes: 0

Aman Aggarwal
Aman Aggarwal

Reputation: 3754

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];

Upvotes: 80

Related Questions