如何将自定义NOT TINTED图像添加到UIBarButtonItem

时间:2015-03-30 08:41:20

标签: ios swift uinavigationbar uibarbuttonitem

我想将图像添加到UIBarButtonItem。

我的代码:

        let button1 = UIBarButtonItem(image: UIImage(named: "icon1"), style: .Plain, target: self, action: "Onb1ClickListener")
    let button2 = UIBarButtonItem(image: UIImage(named: "icon2"), style: .Plain, target: self, action: "Onb2ClickListener")
    let button3 = UIBarButtonItem(image: UIImage(named: "icon3"), style: .Plain, target: self, action: "Onb3ClickListener")

    self.navigationItem.setRightBarButtonItems([button1, button2, button3], animated: false)
    self.navigationItem.setHidesBackButton(true, animated: false)
  1. 这有效,但我得到了蓝色图像。我怎样才能得到没有着色的原始图像?

  2. 如何在UINavigationBar的左侧添加图像(不仅仅是图像的条形按钮)?

  3. 由于

1 个答案:

答案 0 :(得分:5)

UIBarButtonItem中的默认图片呈现案例为UIImageRenderingMode.AlwaysTemplate。因此,您必须使用UIImageRenderingMode.AlwaysOriginal的渲染选项创建图像,并将该图像设置为UIBarButtonItem。所以:

let customImage = UIImage(named: "icon1")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let button1 = UIBarButtonItem(image: customImage, style: .Plain, target: self, action: "Onb1ClickListener")