title: Changed rootViewController but the view of old rootViewController is still in the View Hierarchy
body: <p>I have a basic situation, when the user has been authenticated, I remove and change the current screen (the login screen) to another screen inside the app.</p>
  
  <p>To do this, I use this code:</p>
  
  <pre><code>if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
      print("Window's subviews before removed = \(appDelegate.window?.subviews)")
  
      appDelegate.window?.subviews.forEach { $0.removeFromSuperview() }
  
      print("Window's subviews after removed = \(appDelegate.window?.subviews)")
  
      appDelegate.window?.rootViewController?.view?.removeFromSuperview()
      appDelegate.window?.rootViewController?.removeFromParentViewController()
  
      appDelegate.window?.rootViewController = newRootViewController
  
      print("Window's subviews after changed = \(appDelegate.window?.subviews)")
  }
  </code></pre>
  
  <p>This is the output:</p>
  
  <p><a href="https://i.stack.imgur.com/LM97K.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LM97K.png" alt="enter image description here"></a></p>
  
  <p>This is what the user can see on the device screen - looks very OK:
  <a href="https://i.stack.imgur.com/TiGvN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TiGvN.png" alt="enter image description here"></a></p>
  
  <p>However, it isn't ok in the Debug View Hierarchy tool:</p>
  
  <p><a href="https://i.stack.imgur.com/UcLuA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/UcLuA.png" alt="enter image description here"></a></p>
  
  <p>As you can see, the view of old rootViewController is still there, inside <code>UIWindow</code> but not a subview of it - as the output has indicated.</p>
  
  <p>This behavior seems strange, has anyone experienced this problem yet?</p>