UIBarButtonItem Image is not shown and instead a white rectangle in the size of image is shown, Why?
Asked Answered
R

3

23

With whatever image I try to intialize the UIBarButtonItem, its just showing a white background in the size of the image. Even when I tired in interface builder, the result is the same. All these images when used with other objects works perfectly.

How can I solve this??

Resignation answered 25/5, 2010 at 9:33 Comment(1)
Difficult to tell without seeing your code or IB setupJed
C
22

Only alpha values in the image are used to create the bar button image. Whatever image you provide is converted into a image with shades of white, based on the alpha values.

So given that your image is completely white it is clear you don't have any transparency.

The guidelines have this to say:

  • Use the PNG format.
  • Use pure white with appropriate alpha.
  • Do not include a drop shadow.
  • Use anti-aliasing.
  • If you decide to add a bevel, be sure that it is 90° (to help you do this, imagine a light source positioned at the top of the icon).
  • For toolbar and navigation bar icons, create an icon that measures about 20 x 20 pixels.
  • For tab bar icons, create an icon that measures about 30 x 30 pixels.

Note: The icon you provide for toolbars, navigation bars, and tab bars is used as a mask to create the icon you see in your application. It is not necessary to create a full-color icon.

You can however use a custom view to get a full-colour image as this question shows:

Can I have a UIBarButtonItem with a colored image?

This is a bit over the top though and it would be best to stick to the guidelines and use a normal button with an appropriately formatted image.

Cortezcortical answered 25/5, 2010 at 9:55 Comment(0)
P
17

In ios 7 onwards this will help

UIImage *image = [[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];
Pharyngology answered 12/5, 2015 at 5:57 Comment(0)
A
13

In Swift 3 I found that the following works - here shown as an array of more than one barButtonItem

  let barButtonItem1    = UIBarButtonItem( image: UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal) , style: .plain ,target: self, action: #selector(yourAction))
  let barButtonItem2    = UIBarButtonItem(image: UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal)  , style: .plain, target: self, action: #selector(yourAction))

navigationItem.rightBarButtonItems = [barButtonItem1, barButtonItem2]

This shows x2 barButtons on the rightHandSide. To shown the left simply change to

navigationItem.leftBarButtonItem = [barButtonItem1, barButtonItem2]

Aphra answered 11/11, 2016 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.