Jason
Jason

Reputation: 1067

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.

Upvotes: 18

Views: 29688

Answers (6)

Sudhanshu.B
Sudhanshu.B

Reputation: 636

SWIFT 4

    let back = UIImage(named: "back_white")?.withRenderingMode(.alwaysOriginal)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: back, style:.plain, target: nil, action: nil)

Upvotes: 2

Jerome
Jerome

Reputation: 2152

Swift 3 If the item is missing, you can try this.

  let navigationBar = navigationController?.navigationBar
  let topItem = navigationBar?.topItem
  var navigateimage = UIImage(named: "addConnectionFromSupport")
  navigateimage = navigateimage?.withRenderingMode(.alwaysOriginal)
  topItem?.rightBarButtonItem = UIBarButtonItem(image: navigateimage, style:.plain, target: nil, action: nil)

Upvotes: 0

ronak patel
ronak patel

Reputation: 400

In swift 3.0

    let Navigateimage = UIImage(named: "LogoWithTextSmaller.png")
    Navigateimage = Navigateimage?.withRenderingMode(.alwaysOriginal)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: Navigateimage, style:.plain, target: nil, action: nil)

Upvotes: 0

Ashish
Ashish

Reputation: 3137

Swift 3.0

let btnLogo = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
btnLogo.setTitle("", for: .normal)
btnLogo.backgroundColor = UIColor.clear
btnLogo.layer.cornerRadius = 4.0
btnLogo.layer.masksToBounds = true

var imageLogo = UIImage(named: "LogoWithTextSmaller.png")
imageLogo = imageLogo?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
btnLogo.setImage(imageLogo, for: .normal)

let barButton = UIBarButtonItem(customView: btnLogo)
self.navigationItem.leftBarButtonItem = barButton

swift 2.0

var image = UIImage(named: "Filter")
image = image?.imageWithRenderingMode(UIImageRenderingMode.alwaysOriginal)

Objective-C

 UIImage *image = [[UIImage alloc] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

 UIBarButtonItem *_btnLeftBar = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"LogoWithTextSmaller.png"]
                                                      style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:@selector(yourMethod)];

 self.navigationItem.rightBarButtonItem= _btnLeftBar;

For withRenderingMode(_:) details see below apple documentation link https://developer.apple.com/documentation/uikit/uiimage/1624153-withrenderingmode

Upvotes: 2

CoderJimmy
CoderJimmy

Reputation: 76

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)

Upvotes: 3

bhavik shah
bhavik shah

Reputation: 754

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)

Upvotes: 34

Related Questions