更改UIBarButton的图像

3

我的目标是在程序中通过编程方式更改作为IBOutlet使用的UIBarButton的图像。

我在stackoverflow上找到了这个解决方案change-image-of-uibutton-with-other-image,但在iOS 4.2上不起作用。

有人能帮帮我吗。 Simon

我尝试过:

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];
1个回答

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

3
加载UIImage的简短方法:UIImage *image = [UIImage imageNamed:@"play.png"]; - Tommy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接