gamephase gamephase - 2 months ago 19x
Swift Question

Setting an image as Navigation Bar title

I'm trying to set an image as the title of a navigation bar in swift using the code below. The code builds successfully, but the image does not display. Any advice would be appreciated.

import UIKit

class FirstViewController: UIViewController {

@IBOutlet weak var navigationBar: UINavigationItem!
var convoImage: UIImage!
var convoImageView: UIImageView!

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
self.convoImage = UIImage(contentsOfFile: "conversation.png")
self.convoImageView = UIImageView(image: self.convoImage)
self.navigationBar.titleView = self.convoImageView


Set the navigationItem's titleView.

let image = UIImage(named: "image_name.png")
self.navigationItem.titleView = UIImageView(image: image)

In your code replace below line:

self.convoImage = UIImage(contentsOfFile: "conversation.png")


self.convoImage = UIImage(named: "conversation.png")