title: How to make root navigation bar transparent, but child navigation bars not?
body: <p>I have been looking around on Stack for some guidance on this, but none of the questions have been asked in this depth nor are the answers updated to the latest swift version, or even work on the newest swift version.</p>
  
  <p>Here's what I have:</p>
  
  <ul>
  <li>A navigation controller with two bar items: 
  
  <ul>
  <li>A left button that segues to a view</li>
  <li>A right button that segues to a view</li>
  </ul></li>
  </ul>
  
  <p><strong>My goal:</strong> To make the navigation root view controller's navigation bar transparent (<em>But have buttons and title still visible</em>), but not the child navigation - <em>without</em> weird nuances, like having the previous color flash, or a cut off nav bar (see gif)</p>
  
  <p><strong>Things I've tried:</strong></p>
  
  <p>On the <code>viewWillAppear(_ animated: Bool)</code> function, make it transparent, then upon <code>viewWillDisappear()</code> function, undo it.</p>
  
  <p>Here is my code: </p>
  
  <pre>
  
       override func viewWillAppear(_ animated: Bool) {
              super.viewWillAppear(animated)
              print("Will appear")
              self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
              self.navigationController?.navigationBar.shadowImage = UIImage()
              self.navigationController?.navigationBar.isTranslucent = true
          }
  
          override func viewWillDisappear(_ animated: Bool) {
              super.viewWillDisappear(animated)
  
              self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
              self.navigationController?.navigationBar.shadowImage = nil
              self.navigationController?.navigationBar.isTranslucent = true
          }
  
  </pre>
  
  <p><strong>This doesn't work.</strong>: What this does is it makes the root view controller's nav bar transparent, but then when I segue to another view controller from a bar item, it flashes the original color and then goes back to clear upon loading of the other view.</p>
  
  <p>I also tried one of the suggestions below (from Md):</p>
  
  <pre>
  
      override func viewWillAppear(_ animated: Bool) {
              self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
              self.navigationController?.navigationBar.shadowImage = UIImage()
              self.navigationController?.navigationBar.isTranslucent = true
              self.navigationController!.navigationBar.backgroundColor = UIColor.clear
          }
  
  
          override func viewDidDisappear(_ animated: Bool) {
              self.navigationController!.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
              self.navigationController?.navigationBar.shadowImage = nil
              self.navigationController?.navigationBar.isTranslucent = true
              self.navigationController!.navigationBar.backgroundColor = UIColor.red
  
          }
  </pre>
  
  <p>Here is the <strong>output</strong>: <a href="https://giphy.com/gifs/Bcu1Q6qUSV4jPhWo4P" rel="nofollow noreferrer">https://giphy.com/gifs/Bcu1Q6qUSV4jPhWo4P</a></p>
  
  <p>What are some solutions to this? Or ideas to get around this?</p>
  
  <p>Will update question with more info if needed.</p>
  
  <p>Thanks all!</p>