Take the tour ×

I am trying to set the image on rightBarButtonItem and its fine but the only thing wrong is the background behind this image which has more width than my image. Anybody has any idea how to fix this. Code:

[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"action_btn.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(actionButtonClicked:)] animated:YES];
share|improve this question
try to increase your image width.... –  user08092013 Jul 23 '11 at 18:29
that i can't the image will distort. –  Ashutosh Jul 23 '11 at 18:50
It is possible. You can check my answer as below. –  AppAspect Jul 23 '11 at 18:50
add comment

2 Answers

up vote 10 down vote accepted

Here you can set the image on rightBarButtonItem of NavigationBar as follows:

UIButton *button1 = [[UIButton alloc] init];
[button1 setBackgroundImage:[UIImage imageNamed: @"image1.png"] forState:UIControlStateNormal];
[button1 addTarget:appDelegate action:@selector(Open_Link1) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
[button1 release];

Please let me know if you have any more questions.

share|improve this answer
Here you can try this and It is working for me and I am sure will definitely work for you and you can take necessary actions when it works. –  AppAspect Jul 23 '11 at 18:55
Thanks for the actions. Let me know if you need more help. –  AppAspect Jul 23 '11 at 19:08
add comment

Im not in XCode at the moment, so I cannot post code. The procedure goes like this

  1. Create the UIBarButtonItem with default initializer
  2. Create the UIImage
  3. Get the CGRect from the image dimensions,
  4. Set the UIBarButtonItem.frame to that dimensions

If it doesn't match 100%, consider adding the bounds of the button to the image dimensions before setting the button's frame

share|improve this answer
I don't see any property to set the frame of this button. A little bit of code would be very helpful –  Ashutosh Jul 23 '11 at 18:49
add comment

Your Answer


