我想将图像添加到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)
这有效,但我得到了蓝色图像。我怎样才能得到没有着色的原始图像?
如何在UINavigationBar的左侧添加图像(不仅仅是图像的条形按钮)?
由于
答案 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")