Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 871
budiDino Avatar asked Feb 28 '14 08:02

budiDino


2 Answers

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];
}
like image 165
Toseef Khilji Avatar answered Nov 15 '22 13:11

Toseef Khilji


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
}
like image 24
Hapeki Avatar answered Nov 15 '22 12:11

Hapeki