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

11,527

Solution 1

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

Set this two method in your mapview

MapView.m

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

-(void)viewWillDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

Solution 2

For Swift 3:

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)

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

override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(animated)

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

Related videos on Youtube

budiDino
Author by

budiDino

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

Comments

  • budiDino
    budiDino over 1 year

    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 about 10 years
      add navigationbar image in viewWillDisappear method.
  • Toseef Khilji
    Toseef Khilji about 10 years
    @budidino: Happy to Help you :)
  • ryo
    ryo over 9 years
    viewWillDisappear is the point! I used viewDidDisappear, it is WRONG! Hope that can save other's time.
  • Jeevan Thandi
    Jeevan Thandi over 7 years
    Does anyone experience the navigation bar turning black for a second in this transition?
  • turingtested
    turingtested over 6 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 6 years
    This works for Swift 4 as well: self.navigationController?.navigationBar.setBackgroundImage(‌​nil, for: UIBarMetrics.default)
  • MagicFlow
    MagicFlow about 6 years
    @JeevanThandi yes I do too. Did you find a solution to this?
  • George Sabanov
    George Sabanov over 5 years
    @turingtested Did you find a solution for Large title nav bar?