title: How to hide the iOS app content view when app is not active?
body: <p>I'm trying to add a cover screen to app when app is pushed to background by user. </p>
  
  <p>I have these two methods on AppDelegate to add/remove the cover view.</p>
  
  <pre><code>func showCover(){
    let blackV = UIView(frame: UIApplication.shared.keyWindow?.bounds ?? .zero)
    blackV.backgroundColor = .black
    blackV.tag = kCoverTag
    let imgV = UIImageView(image: UIImage(named: "Logo-White"))
    imgV.center = blackV.center
    blackV.addSubview(imgV)
    UIApplication.shared.keyWindow?.addSubview(blackV)
  }
  
  func hideCover(){
    if let blackV = UIApplication.shared.keyWindow?.subviews.filter({$0.tag == kCoverTag}).first{
       blackV.removeFromSuperview()
     }
  }
  </code></pre>
  
  <p>The tag value is declared on AppDelegate:</p>
  
  <pre><code> let kCoverTag = 987
  </code></pre>
  
  <p>In the <code>applicationWillResignActive</code> , I apply the cover</p>
  
  <pre><code>func applicationWillResignActive(_ application: UIApplication) {
      showCover()
  }
  </code></pre>
  
  <p>and in didBecomeActive, I remove the cover.</p>
  
  <pre><code>func applicationDidBecomeActive(_ application: UIApplication) {
     hideCover()
  }
  </code></pre>
  
  <p>This works fine when app is sent to background by pressing home button.</p>
  
  <p>Right after the <code>applicationDidBecomeActive</code> is called, when I print the subviews of keyWindow , I get the output as:</p>
  
  <pre><code>po UIApplication.shared.keyWindow?.subviews
  ▿ Optional&lt;Array&lt;UIView&gt;&gt;
    ▿ some : 2 elements
      - 0 : &lt;UITransitionView: 0x7f97bd82ffc0; frame = (0 0; 414 896); autoresize = W+H; layer = &lt;CALayer: 0x6000028ecce0&gt;&gt;
      - 1 : &lt;UIView: 0x7f97bae27f20; frame = (0 0; 414 896); tag = 987; layer = &lt;CALayer: 0x600002868400&gt;&gt;
  </code></pre>
  
  <p>This is good as it shows the cover view as one of the subview added to the key window.</p>
  
  <p>Now, when the app is sent to background by calling <code>UIApplication.shared.openURL</code> , the applicationWillResignActive is called as expected. </p>
  
  <p>And when the user press the "Back to app button" , the app will be shown back to user. </p>
  
  <p>Now,when I print the subviews of key window in the <code>didBecomeActive</code> method right before calling <code>hideCover()</code> method, it looks like it has only one subview.</p>
  
  <pre><code>po UIApplication.shared.keyWindow?.subviews
  ▿ Optional&lt;Array&lt;UIView&gt;&gt;
    ▿ some : 1 element
      - 0 : &lt;UITransitionView: 0x7f97bfc05510; frame = (0 0; 414 896); autoresize = W+H; layer = &lt;CALayer: 0x6000029984c0&gt;&gt;
  </code></pre>
  
  <p>Since this shows only one view, the <code>hideCover()</code> method couldn't find the subview and hence couldn't remove it. But in the app I could see the cover view .</p>
  
  <p>I'm wondering why the <code>keyWindow</code> subviews didn't show the added cover view.</p>
  
  <p>I'm testing this in simulator. </p>
  
  <p><strong>Update:</strong></p>
  
  <p>I did two separate print calls</p>
  
  <p>Right after adding the cover view:</p>
  
  <pre><code>print("resign active",UIApplication.shared.keyWindow?.subviews)
  </code></pre>
  
  <p>This show two subviews for keyWindow</p>
  
  <p>and right before removing the cover view in didBecomeActive method:</p>
  
  <pre><code>print("did become active",UIApplication.shared.keyWindow?.subviews)
  </code></pre>
  
  <p>This show only ONE subview for keyWindow</p>