-1

I am trying to put a logo (png) file into my navigation controller. I found one solution that "worked" but it didn't scale down the image to fit, thus is was massive

This is the code im using thus far, but it makes the image huge

let logoImage:UIImage = UIImage(named: "logo.png")!
    self.navigationItem.titleView = UIImageView(image: logoImage)
RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70

2 Answers2

1

You can do it in 2 ways: programmatically or via XCode:

An easy way is via XCode. For it, drag view from the object library and drag it to your navigation bar. Later drag into your view in navigation bar UIImageView from object library. That's all.

Or you can do all of this programmatically.

    var myView = UIView()
    var imageView = UIImageView()
    imageView.image = UIImage(named: "your_image_here")

    myView.addSubview(imageView)
    navigationController?.navigationBar.addSubview(myView)
Orkhan Alizade
  • 7,379
  • 14
  • 40
  • 79
1

Try this:

let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
imageView.contentMode = UIViewContentMode.ScaleAspectFit  //you need to set this.

self.navigationItem.titleView = imageView

And if you want to give height and width then add this:

imageView.frame.size.width = 200
imageView.frame.size.height = 45
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165