Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation bar button image

I am using this code to get a logo on my nav bar.

override func viewDidAppear(animated: Bool) {

    let image = UIImage(named: "LogoWithTextSmaller.png")
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
    }

This is fine, but the logo doesn't have any colour - besides 'blue'. Is it because it is a png file. Is there something I can so it retains the original colours

I have done this:

self.navigationItem.titleView = UIImageView(image: image)

and that brings the image onto the nav bar with the correct colours - but it's in the middle and I want it on the left.

like image 431
Jason Avatar asked Feb 03 '15 12:02

Jason


People also ask

What is a navigation bar button?

The Navigation bar is there to help you navigate your phone. The traditional navigation buttons are the default layout and appear at the bottom of the screen. If you want a full screen design, you can change the Navigation bar's layout to Swipe gestures.

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.


2 Answers

You need to declare that the image stays original all the time. so add the code as below

var image = UIImage(named: "image-name")
image = image?.withRenderingMode(.alwaysOriginal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: nil)
like image 169
bhavik shah Avatar answered Oct 11 '22 10:10

bhavik shah


In Swift 3 the same would be accomplished using the following syntax

var image = UIImage(named: "Filter")
image = image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image:image , style: UIBarButtonItemStyle.plain, target: nil, action: nil)
like image 36
CoderJimmy Avatar answered Oct 11 '22 12:10

CoderJimmy