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
}
Related videos on Youtube
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, 2022Comments
-
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:
Now I have a problem when I go to any other because my navigationBar remains transparent:
How do I restore default settings of the navigationBar's backgroundImage and shadowImage?
-
Bhavesh Nayi over 9 yearsadd navigationbar image in viewWillDisappear method.
-
-
Toseef Khilji over 9 years@budidino: Happy to Help you :)
-
ryo over 8 yearsviewWillDisappear is the point! I used viewDidDisappear, it is WRONG! Hope that can save other's time.
-
Jeevan Thandi over 6 yearsDoes anyone experience the navigation bar turning black for a second in this transition?
-
turingtested over 5 yearsIt 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 over 5 yearsThis works for Swift 4 as well:
self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
-
MagicFlow over 5 years@JeevanThandi yes I do too. Did you find a solution to this?
-
George Sabanov over 4 years@turingtested Did you find a solution for Large title nav bar?