2010-07-21 16 views
1

J'essaie d'ajouter une image à l'UIBarButtonItem que je dois utiliser dans un UIToolbar. J'ai réussi à lire l'image et même à l'afficher avec un UIImageView, mais quand je l'ajoute à l'UIBarButtonItem et ensuite j'ajoute cet élément à la barre UIToolbar, la barre d'outils déplace juste un espace "Blank White" de la taille et la forme de l'image que j'essaie de charger.ajouter l'image à UIBarButtonItem pour UIToolbar

Voici ce que j'essaie.

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]; 

Votre aide sera très appréciée.

Merci!

Shumais Ul Haq

Répondre

3

Autre que vous attendez normalement des choses UIKit, vous pourriez avoir besoin de définir un cadre pour le bouton explicitement. Peut-être que c'est votre problème.

Voici ce que j'ai écrit pour un bouton de retour personnalisé, en catégorie UIBarButtonItem (mais vous pouvez simplement en prendre les pièces dont vous avez besoin). Notez que cela a été utilisé pour la barre de navigation, pas pour la barre d'outils, mais je présume que la mécanique est la même, car il s'agit d'un UIBarButtonItem également. Pour UIToolbar vous pouvez simplement utiliser IB pour le faire correctement au moment de la compilation.

#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]; 
} 

utilisation:

[UIBarButtonItem arrowLeftWithText:@"Back" target:self action:@selector(dismiss)]; 
+0

Merci MVDS !!! Oui, c'était le cadre !! J'ai simplement ajouté cette ligne button2.frame = CGRectMake (0, 0, image.size.width, image.size.height); J'ai passé un temps fou juste à essayer de faire fonctionner ça. Je ne pense pas que je vais oublier la relation Button-Frame jamais !! Merci encore! –