Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't setting shadowImage to nil work for iOS 11?

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?

Initial

enter image description here

like image 808
cyril Avatar asked Dec 28 '17 09:12

cyril


1 Answers

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()
}
like image 99
Lance Samaria Avatar answered Oct 02 '22 01:10

Lance Samaria