Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change image of UIBarButton

My target is to change the Image of an UIBarButton (which i use as IBOutlet) programmatically.

I found this solution on stackoverflow change-image-of-uibutton-with-other-image. but this is not working on iOS 4.2.

Can anyone help me with that. Simon

I tried:

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];
like image 955
endo.anaconda Avatar asked Dec 13 '25 21:12

endo.anaconda


1 Answers

If you want to put an image to a button use following code.

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];
like image 65
Satya Avatar answered Dec 16 '25 21:12

Satya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!