views:

84

answers:

2

Im not sure what im doing wrong. The file name is correct, the style is set to plain. But Im getting a bank white box the size of my image. Im using UINavigationController.

Please assist and thank you thank you in advance.

**FYI I am sorta new to objective c so dont be too hard on me. ;)

 UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
     initWithImage:[UIImage imageNamed:@"channel-guide-button.png"]
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(action:)];


self.toolbarItems = [NSArray arrayWithObjects:toolbarChannelGuideButton, nil];
[toolbarChannelGuideButton release];
A: 

Does channel-guide-button.png belong to project?

You could break this out like this:

UIImage *image = [UIImage imageNamed:@"channel-guide-button.png"];
NSLog(@" image = %p", image);
UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
     initWithImage:image
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(action:)];

or just check your project ;-)

westsider
I checked in my project and yes its in there. I also ran a NSLog it displayed a low level number.
Jordan Brown
image = 0x6b45340 is what it displayed in the console
Jordan Brown
Oh well. FWIW, I usually create UIBarButtonItem with initWithCustomView: and pass in UIButton with image associated with it. I'll post code example tomorrow.
westsider
No need to post example, I already figured that. Look at answer above. Thanks for your help though!
Jordan Brown
Now im just trying to figure out how to position it center on the toolbar
Jordan Brown
UIBarButtonSystemItemFlexibleSpace did the trick. ;)
Jordan Brown
You beat me to it again.
westsider
As you can tell im just barely learning! haha
Jordan Brown
Whats your real name westsider?
Jordan Brown
+4  A: 

The reason it was creating the white mask was because the UIToolBar doesnt allow color images on it by default. The way to accomplish this is creating a UIImage then assign a UIButton to that image. Then create a UIBarButton using initWithCustomView with the UIButton as the custom view. Code:

     //Load the image   
     UIImage *buttonImage = [UIImage imageNamed:@"your-image.png"];

     //create the button and assign the image
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     [button setImage:buttonImage forState:UIControlStateNormal];

     //sets the frame of the button to the size of the image
     button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

     //creates a UIBarButtonItem with the button as a custom view
     UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];



     self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil];
     [customBarItem release];
Jordan Brown