gamephase gamephase - 1 year ago 92
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

Answer Source

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")