1

I'm trying to add my image on UINavigationItem using this code:

override func viewDidLoad() {
        super.viewDidLoad()

        self.navItemTop.titleView?.contentMode = UIViewContentMode.scaleToFill

        self.navItemTop.titleView? = ViewController4.navigationImage! //my image, its correct for sure

        self.navigationController?.navigationBar.barTintColor = UIColor.white

    }

When i go to another UIViewController and then go back, image appears and disappears a second later. I've tried to put this piece of code in viewWillAppear() method, but result is the same. How can i fix this?

coldembrace
  • 549
  • 8
  • 19

2 Answers2

0

You need to set image in the viewWillAppear: method.

Reason: When you are push or Present Sometime there is possibility that we can set another image or clear color to Nav. Bar that's why viewDidLoad: method not called on back you lost your old settings of nav. Bar.

Mukesh Lokare
  • 2,159
  • 26
  • 38
0

You can add it inside UIImageView:-

let image = UIImage(named: "yourimage.png")
let imageView = UIImageView(image:image)
self.navigationItem.titleView = imageView
Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38