I have a navigation bar with prefersLargeTitles
set to True
.
When clicking on a cell in my table view to show a detail view, I want the navigation bar to become transparent using the following, which works fine:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
When pressing the back button on the detail view to return to the main view controller, I call:
self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = nil
Unfortunately, the shadow view of the navigation bar won't return as shown below. What am I missing?
This is what worked for me in Xcode 10.1, iOS 12.1.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 1. hide it in the current view controller you want it hidden in
navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
navigationController?.navigationBar.layoutIfNeeded()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
// 2. show it in when pushing or popping in the next view controller
navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
navigationController?.navigationBar.setValue(false, forKey: "hidesShadow")
navigationController?.navigationBar.layoutIfNeeded()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With