iPhone SDK:如何将图像添加到UIBarButton?

时间:2010-05-27 19:01:25

标签: iphone

我使用下面的代码在导航栏上创建一个带图像的按钮。

我可以看到图像,但我也可以看到它周围按钮的边框。我的问题是,如何摆脱按钮边框。我只想看到导航栏上的图像,没有边框。

UIBarButtonItem *settingsBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_prefs.png"] style:UIBarButtonItemStylePlain target:self action:@selector(openSettings:)];
[[self navigationItem] setLeftBarButtonItem:settingsBtn];
[settingsBtn release];

提前致谢。任何指针,进一步阅读的链接或赞赏的例子。

1 个答案:

答案 0 :(得分:20)

这是我目前的一个项目的代码片段。它会加载UIBarButtonItem的透明度图片:

UIImage* image = [UIImage imageNamed:@"some-image.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];
UIBarButtonItem* someBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setRightBarButtonItem:someBarButtonItem];
[someBarButtonItem release];
[someButton release];