改变UIBarButton的形象

end*_*nda 3 iphone image uibarbuttonitem ios4

我的目标是以编程方式更改UIBarButton的图像(我用作IBOutlet).

我在stackoverflow上找到了这个解决方案,改变了uibutton-with-other-image的图像.但这不适用于iOS 4.2.

任何人都可以帮助我.西蒙

我试过了:

UIImage *image = [UIImage imageWithContentsOfFile: 
                 [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]];
playButton.image = image;
Run Code Online (Sandbox Code Playgroud)
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];
Run Code Online (Sandbox Code Playgroud)

Sat*_*tya 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];
Run Code Online (Sandbox Code Playgroud)

  • 一个加载UIImage的简短方法`UIImage*image = [UIImage imageNamed:@"play.png"]; (3认同)