【问题标题】:add image to UIBarButtonItem using initWithImage:(UIImage *)image使用 initWithImage:(UIImage *)image 将图像添加到 UIBarButtonItem
【发布时间】:2010-07-21 14:48:49
【问题描述】:

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

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

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

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

谢谢!

【问题讨论】:

  • 你能 NSLog 你的图片以确保它不是 nil
  • 图像不为零,我已将图像添加到 UIImageview 并显示在屏幕上。任何人都可以验证是否存在与 UIBarButtonItem 一起使用的特殊类型的 .png 格式???
  • @ShumaisUlHaq 似乎这个问题需要一个最佳答案。请看一看。

标签: iphone uibarbuttonitem


【解决方案1】:

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

Objective-C

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

斯威夫特 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(_:)))

【讨论】:

  • 这修复了我的错误。谁能解释为什么这是必要的?
  • 我尝试了一切,这是唯一有效的解决方案。
【解决方案2】:

这是一个从 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];

【讨论】:

  • 谢谢,它对我来说工作正常,我建议使用这个代码
  • 如果您想向此按钮添加目标操作,它会更好,甚至更好。这样做 [face addTarget:self action:@selector(faceBtnPressed) forControlEvents:UIControlEventTouchUpInside]
【解决方案3】:

我会尝试设置

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

【讨论】:

  • 为什么这不是顶帖?
  • 我错过了什么吗?问题和这篇文章中的代码没有区别?!!
【解决方案4】:

来自documentation of UIBarButtonItem

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

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

【讨论】:

  • 看看我下面的两个解决方案,他们解决了这个问题。
【解决方案5】:

Swift 中:

第一个解决方案

let img = UIImage(named: "picture")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = leftBarButtonItem

第二种解决方案

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)

【讨论】:

    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

        UIBarbuttonItem initWithImage 不会使用指定的图像创建条形按钮。 它使用给定的图像创建 UIBarbutton 的模式。如果指定的图像不透明,它将显示一个带有指定 tintcolor 的条形按钮,该按钮具有图像的大小。 如果图像有不透明部分,则色调颜色将填充在不透明部分上,如默认的 UIBarButtonSystemItemBookmarks。它具有不透明的边框和透明的内侧。

        在您的情况下,titincolor 是白色的,这就是它显示白色的原因。

        【讨论】:

          【解决方案9】:

          我会写和你一样的代码。请注意,当您将图像添加到项目时,请确保将其添加到您正在构建的目标

          【讨论】:

            【解决方案10】:

            您可以使用system standard icons 创建UIBarButtonItem 使用
            - initWithBarButtonSystemItem:target:action:

            例如:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-12-01
              • 2013-05-09
              • 1970-01-01
              • 2016-11-24
              • 2011-06-18
              相关资源
              最近更新 更多