Charles
Charles

Reputation: 413

How can you change the navigation bar title to an image? Other methods have not worked

I have tried the code below and the answer to multiple other questions on Stackoverflow and I still cannot get this to work.. I know the image Profile exists in my assets folder. I have made outlets from my navigation bar and navigation item and tried using that but still got nothing. At most the text I have on the title will disappear...I feel as though I am missing something all together.

Located in ViewController's ViewDidLoad function

    let image = UIImage(named: "Profile")
    let imageView = UIImageView(image: image)
    self.navigationItem.titleView = imageView
    self.navigationItem.titleView?.sizeToFit()

Thanks for your help!

Upvotes: 1

Views: 221

Answers (2)

Vikas Dadheech
Vikas Dadheech

Reputation: 1720

In Objective C:

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];

where "image" is the instance of the image that you want to set as title view.

Upvotes: -1

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

initially set the frame for imageview

 let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
 imageView.contentMode = .ScaleAspectFit

 if  let image = UIImage(named: "Profile")
{
 imageView.image = image

 }else
  {
   imageView.image =  UIImage(named: "Profile.png")
   }
   self.navigationItem.titleView = imageView

Upvotes: 2

Related Questions