5

我不确定我做错了什么。文件名正确,样式设置为plain。但是我得到了一个和我的图像一样大小的银行白盒。我正在使用 UINavigationController。

请协助并谢谢您提前谢谢。

**仅供参考,我对目标 c 有点陌生,所以不要对我太苛刻。;)

 UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
     initWithImage:[UIImage imageNamed:@"channel-guide-button.png"]
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(action:)];


self.toolbarItems = [NSArray arrayWithObjects:toolbarChannelGuideButton, nil];
[toolbarChannelGuideButton release];
4

3 回答 3

9

它创建白色蒙版的原因是UIToolBar默认情况下不允许在其上使用彩色图像。实现这一点的方法是创建一个UIImage然后将一个分配UIButton给该图像。然后创建一个UIBarButton使用initWithCustomView作为UIButton自定义视图。

代码:

     //Load the image   
     UIImage *buttonImage = [UIImage imageNamed:@"your-image.png"];

     //create the button and assign the image
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     [button setImage:buttonImage forState:UIControlStateNormal];

     //sets the frame of the button to the size of the image
     button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

     //creates a UIBarButtonItem with the button as a custom view
     UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];



     self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil];
     [customBarItem release];
于 2010-10-27T01:52:59.057 回答
1

从 iOS 7 开始,您可以在下面使用:

 UIImage *image = [[UIImage imageNamed:@"myImage.png"];
 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];
于 2015-05-12T06:00:16.977 回答
-1

channel-guide-button.png 是否属于项目?

你可以这样打破:

UIImage *image = [UIImage imageNamed:@"channel-guide-button.png"];
NSLog(@" image = %p", image);
UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
     initWithImage:image
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(action:)];

或者只是检查你的项目;-)

于 2010-10-27T00:54:23.943 回答