我的目标是以编程方式更改 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];
最佳答案
如果您想将图像放入按钮,请使用以下代码。
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];
关于iphone - 更改 UIBarButton 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4345562/