Restore navigationBar background image after setting it to [UIImage new]


Solution 1

Set nil for image of navigation Controller on viewWillDisappear on map view

Set this two method in your mapview


    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];

Solution 2

For Swift 3:

override func viewWillAppear(_ animated: Bool) {

  self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
  self.navigationController?.navigationBar.shadowImage = UIImage()

override func viewWillDisappear(_ animated: Bool) {

  self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
  self.navigationController?.navigationBar.shadowImage = nil

Related videos on Youtube

Author by


I was a Visual Basic 6 game developer. After that I went to web development using php, javascript and MySQL. Now I'm doing iPhone apps using Swift but considering career change to product and UX.

Updated on September 16, 2022


  • budiDino
    budiDino 9 months

    I needed a completely transparent navigation bar for the mapView so I did this:

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

    That returns the desired effect, as seen here:

    enter image description here

    Now I have a problem when I go to any other because my navigationBar remains transparent:

    enter image description here

    How do I restore default settings of the navigationBar's backgroundImage and shadowImage?

    • Bhavesh Nayi
      Bhavesh Nayi over 9 years
      add navigationbar image in viewWillDisappear method.
  • Toseef Khilji
    Toseef Khilji over 9 years
    @budidino: Happy to Help you :)
  • ryo
    ryo over 8 years
    viewWillDisappear is the point! I used viewDidDisappear, it is WRONG! Hope that can save other's time.
  • Jeevan Thandi
    Jeevan Thandi over 6 years
    Does anyone experience the navigation bar turning black for a second in this transition?
  • turingtested
    turingtested over 5 years
    It doesn't work together with large titles in iOS 11 - hopefully a bug that will be fixed. [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault] doesn't restore the status bar.
  • Cole Roberts
    Cole Roberts over 5 years
    This works for Swift 4 as well: self.navigationController?.navigationBar.setBackgroundImage(‌​nil, for: UIBarMetrics.default)
  • MagicFlow
    MagicFlow over 5 years
    @JeevanThandi yes I do too. Did you find a solution to this?
  • George Sabanov
    George Sabanov over 4 years
    @turingtested Did you find a solution for Large title nav bar?