1

I'm importing a .PNG into xcode. The image has transparency. When it's rendered on a button from the ID, all the content of the image except the transparency turns to white.

Is this a known issue? Is it because of the way I save the .PNG?

Thanks for the help!

Theraot
  • 31,890
  • 5
  • 57
  • 86
Vince
  • 11
  • 2
  • Does the .png look correct when viewing it elsewhere (such as Preview)? If you post your relevant code, it might be easier to spot the issue. – AtkinsonCM Feb 10 '12 at 21:28
  • Wait a minute - are you talking about UIButton or UIBarButtonItem? It sound like the latter... does the image show properly if you load it into a UIImageView? – Rok Jarc Feb 10 '12 at 22:02
  • yes, the image looks just fine in the preview (before importing the image in xcode), but not in the interface builder, where I set the image on the button properties – Vince Feb 10 '12 at 22:11
  • Yes, it is a UIBarButtonItem and it does display correctly if it's UIImageView – Vince Feb 10 '12 at 22:11

2 Answers2

0

To display an image in a UIBarButtonItem, you need to create a UIButton with the image, then add that button to the UIBarButtonItem. Otherwise you just get the outline, as you mentioned.

So:

// Initialize the UIButton with your image
UIImage *myImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"someImage" ofType:@"png"]];
UIButton *myButton = [[UIButton alloc] init];
[myButton setImage:myImage forState:UIControlStateNormal];

//Then create the UIBarButton
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
AtkinsonCM
  • 376
  • 1
  • 14
0

That's just how images work on UIBarButtonItems and UITabBarItems: the image you supply is only used as a transparency mask. You can either use an UIImageView/UIButton instead to display your image (as AtkinsonCM has suggested), or create an image that will work as a mask.

I personally feel it's usually better to play by Apple's rules, unless you have a compelling reason not to (e.g. the user experience will be significantly improved).

Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137