向UINavigationBar添加按钮

时间:2016-03-12 22:32:06

标签: swift uinavigationbar

我想在我的navigationBar中添加图片,但我无法正确完成。这是代码:

@IBOutlet weak var btnStateChange: UIBarButtonItem!
    let buttonState: UIButton = UIButton(type: .Custom)
    buttonState.setImage(UIImage(named: "Button-State.png"), forState: UIControlState.Normal)
    buttonState.addTarget(self, action: nil, forControlEvents: .TouchUpInside)
    buttonState.frame = CGRectMake(0, 0, 22, 22)

    let barButton = UIBarButtonItem(customView: buttonState)
    self.btnStateChange = barButton

应该看起来像这样:

enter image description here

但我得到了这个结果:

enter image description here

我需要更改代码吗?或者它可能是我使用的PNG?

1 个答案:

答案 0 :(得分:1)

如果绿色是png图像的一部分,您可能需要将渲染模式设置为.AlwaysOriginal的{​​{1}}。

UIImage