views:

292

answers:

2

Hi,

I am trying to add an image to the UIBarButtonItem which I have to use in a UIToolbar.

I have managed to read the image and even get it to display with a UIImageView, but when i add it to the UIBarButtonItem and then add that item to the UIToolbar, the toolbar just displaces a "Blank White" space the size and shape of the image that I am trying to load.

here is what I am trying.

UIImage *image = [UIImage imageNamed:@"6.png"];

//This is the UIImageView that I was using to display the image so that i know that it is being read from the path specified.  
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 50, image.size.width, image.size.height);
[self.view addSubview:imageView];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:image forState:UIControlStateNormal];

//This is the first way that I was trying to accomplish the task but i just get a blank white space

//This is the Second way but with the same blank white result.
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithCustomView:button2];

NSArray *items = [NSArray arrayWithObjects: systemItem1, nil];

//Adding array of buttons to toolbar
[toolBar setItems:items animated:NO];

//Adding the Toolbar to the view.
[self.view addSubview:toolBar];

Your help will be much appreciated.

Thank You!

Shumais Ul Haq

+1  A: 

Other than you'd normally expect in UIKit stuff, you might need to set a frame for the button explicitly. Maybe that's your problem.

This is what I wrote for a custom styled back button, as a category to UIBarButtonItem (but you can just take the pieces you need from it).

Note that this was used for the navigation bar, not the toolbar, but I presume the mechanics are the same, since it is a UIBarButtonItem as well. For UIToolbar you can just use IB to get it right at compile time.

#define TEXT_MARGIN 8.0f
#define ARROW_MARGIN 12.0f
#define FONT_SIZE 13.0f
#define IMAGE_HEIGHT 31.0f

+(UIBarButtonItem*)arrowLeftWithText:(NSString*)txt target:(id)target action:(SEL)selector
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [[UIImage imageNamed:@"arrow_left.png"]
        stretchableImageWithLeftCapWidth:15 topCapHeight:0];

    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchDown];

    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [btn setContentEdgeInsets:UIEdgeInsetsMake(0.0f,0.0f,0.0f,TEXT_MARGIN)];
    [btn.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FONT_SIZE]];
    [btn.titleLabel setShadowOffset:CGSizeMake(0.0f,-1.0f)];

    /**** this is the magic line ****/
    btn.frame = CGRectMake(0.0f,0.0f,
        [txt sizeWithFont:[btn.titleLabel font]].width+ARROW_MARGIN+TEXT_MARGIN,
        IMAGE_HEIGHT);

    [btn styleBarButtonForState:UIControlStateNormal withImage:img andText:txt];
    [btn styleBarButtonForState:UIControlStateDisabled withImage:img andText:txt];
    [btn styleBarButtonForState:UIControlStateHighlighted withImage:img andText:txt];
    [btn styleBarButtonForState:UIControlStateSelected withImage:img andText:txt];
    return [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
}

usage:

[UIBarButtonItem arrowLeftWithText:@"Back" target:self action:@selector(dismiss)];
mvds
Thank You mvds!!!Yes, it was the frame!!I simply added this line button2.frame = CGRectMake(0, 0, image.size.width, image.size.height);I spent an insane amount of time just trying to get this to work. I dont think that I am going to forget the Button-Frame relationship ever!!Thanks again!
Shumais Ul Haq
A: 

The approach mentioned by mvds, above, works as follows

    UIImage *image = [UIImage imageNamed:@"6.png"];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setImage:image forState:UIControlStateNormal];
    button2.frame = CGRectMake(0, 0, image.size.width, image.size.height);

    UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithCustomView:button2];

But, I would also like to know if it could be done by using InitWithImage when creating a UIBarButtonItem. Can someone also suggest a way to solve this problem by using the following approach:

    UIImage *image = [UIImage imageNamed:@"6.png"];

    UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(pp:)];

Thank You!

Shumais Ul Haq
I am more interested in the initWithImage approach of the UIBarButtonItem , because the image that is supplied will be automatically scaled down to the appropriate size.this is not the case with the solution mentioned by mvds, although it works it displays the image in full size and does not automatically scale it down.
Shumais Ul Haq
this should be a new question, or a modificatiom to your original question, but not an *answer* to your own question! but to answer: it could be possible but you have seen how tricky things can get. Go and Experiment!
mvds
you shouldn't resize images on such scales, it will be very ugly and (it being run on a phone) a waste of cpu.
mvds
Sorry. I have selected the check mark besdies your answer, i hope that is how we mark it answered.i am not trying to resize images, Now i want the image to be displayed on the toolbar so that when i press the button the glow effect can be seen, currently with the solution it does not do that and simply darkens the image to show that it is selected.I am starting a new question for this other part.thanks.
Shumais Ul Haq
thanks! See my answer, there's 4 lines of `styleBarButtonForState:` which set the image for the different states, which is what you are looking for.
mvds
Hey mvds, I have a UIButton but it doesnt have the styleBarButtonForState option in it???? UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];button styleBarButtonForState:UIControlStateNormal withImage:image andText:nil]; when i compile it, it gives a me a warning theat UIButton may not respond to styleBarButtonForState:
Shumais Ul Haq