2010-12-03 18 views
3

Mon objectif est de changer l'image d'un UIBarButton (que j'utilise comme IBOutlet) par programmation. J'ai trouvé cette solution sur stackoverflow change-image-of-uibutton-with-other-image. mais cela ne fonctionne pas sur iOS 4.2.Changer l'image de UIBarButton

Quelqu'un peut-il m'aider avec cela. Simon

j'ai essayé:

UIImage *image = [UIImage imageWithContentsOfFile: 
       [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]]; 
playButton.image = image; 

UIImage *image = [UIImage imageWithContentsOfFile: 
       [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]];UIImage *image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]]; 
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height); 
UIButton* someButton = [[UIButton alloc] initWithFrame:frame]; 
[someButton setBackgroundImage:image forState:UIControlStateNormal]; 
[someButton setShowsTouchWhenHighlighted:YES]; 
playButton = [[UIBarButtonItem alloc]initWithCustomView:someButton]; 

[someButton release]; 

Répondre

6

Si vous voulez mettre une image à une utilisation de bouton code suivant.

UIImage *image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]]; 

CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height); 

UIButton* someButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[someButton setBackgroundImage:image forState:UIControlStateNormal]; 
[someButton setShowsTouchWhenHighlighted:YES]; 
playButton = [[UIBarButtonItem alloc]initWithCustomView:someButton]; 
+3

une méthode plus courte pour charger UIImage 'UIImage * image = [UIImage imageNamed: @" play.png "];' – Tommy