Reputation: 3255
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:
But I get this result:
Do I need to change the code? Or is it possibly the PNG I use?
Upvotes: 0
Views: 72
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