1

I try to show a png image on my navigation panel. I do as following:

UIImage *img = [UIImage imageNamed:@"1.png"];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(myAction)];
self.navigationItem.rightBarButtonItems = @[myButton];

The img isn't nil, however myButton is shows as a white square. The size of this square is the same as the size of my image, but the image isn't shown, just a white square. I have a @2x version of my image for retina and I selected my project as a target for both these images.

What can be wrong?

Azat
  • 6,745
  • 5
  • 31
  • 48
Mirimon
  • 102
  • 5
  • Have you sized it accordingly [this SO answer](http://stackoverflow.com/a/1591139/1565335)? – Azat May 09 '15 at 22:32
  • Read the document, "Toolbars", particularly the section entitled, "Bar Button Item Icons". This will explain your problem. – rdelmar May 09 '15 at 22:47

2 Answers2

1

Check Human Interface Guidelines for Toolbar and navigation bar icon (optional) :

Regardless of the icon’s visual style, create a toolbar or navigation bar icon in the following sizes :

About 22 x 22 pixels (standard resolution)

About 44 x 44 pixels (2x)

About 66 x 66 pixels (3x)

Code :

UIImage *image = [UIImage imageNamed:@"image.png"];
UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithImage:newImage style:UIBarButtonItemStylePlain target:self action:@selector(myAction)];
self.navigationItem.rightBarButtonItems = @[myButton];
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
0
UIImage *img = [UIImage imageNamed:@"1"];**//1.png previously**

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(myAction)];
self.navigationItem.rightBarButtonItems = @[myButton];

Just remove .png simply give the image name and it will work.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Amol Hirkane
  • 75
  • 1
  • 11