Iphone 使用initWithImage:(UIImage*)图像将图像添加到UIBarButtonim

Iphone 使用initWithImage:(UIImage*)图像将图像添加到UIBarButtonim,iphone,uibarbuttonitem,Iphone,Uibarbuttonitem,我想知道如何在创建UIBarButtonim时使用InitWithImage将图像设置为UIBarButtonim,然后将其添加到UIToolbar 我正在执行以下操作,但它会在图像应该位于UIToolbar上的位置创建一个空白空白 UIImage *image = [UIImage imageNamed:@"6.png"]; UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:image style:UI

我想知道如何在创建UIBarButtonim时使用InitWithImage将图像设置为UIBarButtonim,然后将其添加到UIToolbar

我正在执行以下操作,但它会在图像应该位于UIToolbar上的位置创建一个空白空白

UIImage *image = [UIImage imageNamed:@"6.png"];

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(pp:)];
谢谢大家!

我会尝试设置

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pp:)];

我也遇到了这个问题,通过使用带有alpha通道的png文件解决了这个问题。
我试着做一个快速的模型,并在背景上添加了一个带有jpg文件的后退按钮,这是按钮的默认颜色。我有一个白色的长方形。但是,当我在透明背景上创建了一个箭头并将其保存为png文件时,它成功了

下面是一个从Facebook徽标创建工具栏按钮的示例。 创建指向图像(已添加到xCode的文件)的指针,创建自定义按钮,更改按钮的大小以匹配图像,设置按钮的图像,从按钮视图创建工具栏按钮

UIImage *faceImage = [UIImage imageNamed:@"facebook.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];
从:

条形图上显示的图像源自此图像。如果此图像太大而无法在条形图上显示,则会将其缩放以适合。通常,工具栏和导航栏图像的大小为20 x 20点。源图像中的alpha值用于创建图像。忽略不透明值


换句话说,UIBarButton项将忽略图像的颜色,并根据当前的条形图样式对其重新着色。显示图像的唯一方法是使用不同的alpha值绘制图像。由于图像的所有像素的alpha值均为1,因此它们都以最大亮度绘制,并获得一个空白

UIBarButtonim initWithImage不会使用指定的图像创建条形按钮。 它使用给定的图像创建UIBarbutton的模式。如果指定的图像不是透明的,它将显示一个带有指定颜色的条形按钮,其大小与图像相同。 如果图像有不透明部分,则着色颜色将填充在不透明部分上,如默认UIBarButtonSystemItemBookmarks。它包含不透明边框和透明内侧


在您的情况下,标题颜色是白色的,这就是为什么它显示为白色。

对于iOS 7+您可以执行以下操作:

目标-C

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

 let image = UIImage(named: "myImage")?.imageWithRenderingMode(.AlwaysOriginal)
 let button = UIBarButtonItem(image: image, style: .Plain, target: self, action: #selector(YOUR_METHOD(_:)))
Swift 3.0

let image = UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal)
let button = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(YOUR_METHOD(_:)))

我会写和你一样的代码。注意:将图像添加到项目中时,请确保将其添加到要根据其进行构建的目标中。

创建条形按钮项后,请执行以下操作:

systemItem1.image = [systemItem1.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

在Swift中:

第一个解决方案

第二种解决方案


您可以使用
-initWithBarButtonSystemItem:target:action:

例如:

[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(YOUR_METHOD:)];

你能记录你的图像以确保它不是零吗?图像不是零,我已将图像添加到UIImageview并在屏幕上显示。有人能验证是否有一种特殊类型的.png格式适用于uibarbuttonite吗?@ShumaisUlHaq似乎这个问题需要一个最好的答案。请看一看。谢谢,它对我来说很好,我建议使用此代码。如果你想将目标操作添加到此按钮,它很好,甚至更好。执行此操作[face addTarget:self action:@selector(faceBtnPressed)forControlEvents:UIControlEventTouchUpInside]为什么这不是最上面的帖子?我遗漏了什么吗?问题和这篇文章中的代码没有区别?!!看看我下面的两个解决方案,它们解决了这个问题。这解决了我的错误。有人能解释为什么这是必要的吗?我尝试了一切,这是唯一有效的解决方案。
let img = UIImage(named: "picture")!
let imgWidth = img.size.width
let imgHeight = img.size.height
let button = UIButton(frame: CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight))
button.setBackgroundImage(img, forState: .Normal)
button.addTarget(self, action: nil, forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(YOUR_METHOD:)];