iphone - 更改 UIBarButton 的图像

标签 iphone image ios4 uibarbuttonitem

我的目标是以编程方式更改 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/

相关文章:

iphone - 为什么在 iOS6 上访问 SQLite 数据库需要更长的时间?

作为对象/类的 Java 图形

ios - 边框半径在 ios4 中不起作用?

Xcode 4.0.1 "Don' 没有权限查看“旧项目文件 - 帮助!

iphone - 如何确保应用程序已正确签名

iphone - 禁用 UIWebView 中特定 HTML 元素的数据检测器

iphone - 将 png/pdf 文件存储在 sqlite 中

image - 如何在 React Native 中清除图片的磁盘缓存

javascript - 查找两个图像中的哪一个被单击并将其报告给数据库

iphone - iOS 3 设备上的 iOS 4 代码