sesc360
sesc360

Reputation: 3255

Adding a button to the UINavigationBar

I want to add an image to my navigationBar, but I can't get it done properly. This is the code:

@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

It is supposed to look like this:

enter image description here

But I get this result:

enter image description here

Do I need to change the code? Or is it possibly the PNG I use?

Upvotes: 0

Views: 72

Answers (1)

Allen
Allen

Reputation: 1734

If green colour is a part of the png image, you probably need to set rendering mode as .AlwaysOriginal for the UIImage.

let buttonState: UIButton = UIButton(type: .Custom)
buttonState.setImage(UIImage(named: "Button-State.png")?.imageWithRenderingMode(.AlwaysOriginal), forState: UIControlState.Normal)
buttonState.addTarget(self, action: nil, forControlEvents: .TouchUpInside)
buttonState.frame = CGRectMake(0, 0, 22, 22)

Upvotes: 1

Related Questions