title: Add a subview as a background to a Navigation Bar
body: <p>I'm using a Navigation Controller in my application and I'm trying to make a progress bar as the background of my Navigation Bar. I made a subclass of UIView to make my progress bar, this works fine.</p>
  
  <p>Now when I build my Navigation Bar Items, I add my progress bar as a SubView of my navigation bar, as well as 2 buttons. This works fine too!</p>
  
  <p>The problem is when I click in a row of my tableview, this makes a segue to a View Controller, and when I click the "Back" button, my progress bar in now on top of the buttons in my Navigation Bar.</p>
  
  <p>Here's the code where I setup the navigation bar items :</p>
  
  <pre><code>    let progressBar = ScrollProgress(frame: CGRect(x: 0, y: -20, width: self.navigationController!.navigationBar.frame.width, height: self.navigationController!.navigationBar.frame.height + 20), section: nil)
      progressBar.tag = 10
      progressBar.pourcentage = 92
  
      self.navigationController!.navigationBar.addSubview(progressBar)
  
      let menuButton = UIBarButtonItem(title: "Menu", style: UIBarButtonItemStyle.Plain, target: revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
      self.navigationItem.leftBarButtonItem = menuButton
  
      let favorisButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: revealViewController(), action: #selector(SWRevealViewController.rightRevealToggle(_:)))
      self.navigationItem.rightBarButtonItem = favorisButton
  
      self.navigationItem.title = "Demo App"
  </code></pre>
  
  <p>You can see here that it works fine, the progress bar covers 92% of the width of my navigation bar, and is UNDER the buttons and title :</p>
  
  <p><a href="https://i.stack.imgur.com/dUCTp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dUCTp.png" alt="enter image description here"></a></p>
  
  <p>Then when I click on a row of my tableview, this performs the segue to another ViewController, it works fine :</p>
  
  <p><a href="https://i.stack.imgur.com/CB6UF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CB6UF.png" alt="enter image description here"></a></p>
  
  <p>When I click the back button (Retour), my progress bar appears on top...</p>
  
  <p><a href="https://i.stack.imgur.com/ff89H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ff89H.png" alt="enter image description here"></a></p>
  
  <p>I can't find out why the subviews order is changing. I tried to replace my subview to the correct index with <code>self.navigationController!.navigationBar.exchangeSubviewAtIndex</code>, it works but the title is flickering at the end of the transition (but the buttons are not flickering).</p>
  
  <p>Is there another way to do this, or am I doing something wrong?</p>
  
  <p>Any help would be appreciated!</p>