我的目标是以编程方式更改UIBarButton的图像(我用作IBOutlet)。
我在stackoverflow change-image-of-uibutton-with-other-image上找到了这个解决方案。但这不适用于iOS 4.2。
任何人都可以帮助我。 西蒙
我试过了:
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];
答案 0 :(得分:6)
如果您想将图像放入按钮,请使用以下代码。
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];