Iphone 使用UIBarButtonim将图像添加到工具栏时出现问题,显示空白的白色框而不是图像

Iphone 使用UIBarButtonim将图像添加到工具栏时出现问题,显示空白的白色框而不是图像,iphone,objective-c,uinavigationcontroller,uibarbuttonitem,toolbaritems,Iphone,Objective C,Uinavigationcontroller,Uibarbuttonitem,Toolbaritems,我不确定我做错了什么。文件名正确,样式设置为“普通”。但是我得到了一个和我的照片一样大的银行白盒子。我正在使用UINavigationController 请协助并提前感谢您 **仅供参考,我对目标c有些陌生,所以不要对我太苛刻 channel-guide-button.png是否属于project 你可以这样打破这个局面: UIImage *image = [UIImage imageNamed:@"channel-guide-button.png"]; NSLog(@" image = %p

我不确定我做错了什么。文件名正确,样式设置为“普通”。但是我得到了一个和我的照片一样大的银行白盒子。我正在使用UINavigationController

请协助并提前感谢您

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


channel-guide-button.png是否属于project

你可以这样打破这个局面:

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

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

创建白色遮罩的原因是默认情况下,
UIToolBar
不允许在其上显示彩色图像。实现这一点的方法是创建一个
ui图像
,然后为该图像指定一个
ui按钮
。然后使用
initWithCustomView
UIButton
作为自定义视图创建一个
UIBarButton

代码:

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

从iOS 7开始,您可以使用以下功能:

 UIImage *image = [[UIImage imageNamed:@"myImage.png"];
 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];

我登记了我的项目,是的,它在那里。我还运行了一个NSLog,它显示了一个低级别的数字。image=0x6b45340是它在控制台中显示的值。FWIW,我通常使用initWithCustomView:创建UIBarButtonim,并传入UIButton和与之关联的图像。明天我会发布代码示例。不需要发布示例,我已经想到了。看看上面的答案。谢谢你的帮助!现在,我只是想弄清楚如何将它定位在工具栏UIButtonTypec的中心位置。我为自己解决了这个问题。我用的是roundedrect one
 UIImage *image = [[UIImage imageNamed:@"myImage.png"];
 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];