Problème d'ajouter une image à la barre d'outils en utilisant UIBarButtonItem, l'affichage boîte blanche vide au lieu de l'image

StackOverflow https://stackoverflow.com/questions/4028927

Question

Je ne suis pas sûr de ce que im faire mal. Le nom de fichier est correct, le style est réglé sur plaine. Mais Im obtenir une boîte blanche bancaire la taille de mon image. Im en utilisant UINavigationController.

S'il vous plaît aider et merci merci à l'avance.

** Je suis FYI sorta nouveau à c objectif donc ne soyez pas trop dur pour moi. ;)

 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];
Était-ce utile?

La solution

La raison pour laquelle il a été crée le masque blanc était parce que le doesnt UIToolBar permet des images couleur sur par défaut. La façon d'y arriver est de créer un UIImage puis attribuez-lui un UIButton à cette image. Ensuite, créez un UIBarButton en utilisant initWithCustomView avec le UIButton comme la vue personnalisée.

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

Autres conseils

A partir de iOS 7, vous pouvez utiliser ci-dessous:

 UIImage *image = [[UIImage imageNamed:@"myImage.png"];
 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];

canal-guide-button.png situez-vous au projet?

Vous pourriez briser ce comme ceci:

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

ou tout simplement vérifier votre projet; -)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top