title: Set UIImageView as background - Swift 4
body: <p>I want to set a background image behind everything that already exists in the view.</p>
  
  <p>I tried :</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
      backgroundImage.image = UIImage(named: "bg-img")
      backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
      self.view.insertSubview(backgroundImage, at: 0)
  }
  </code></pre>
  
  <p>But it didn't work (it didn't even add the img). So I imported the ImageView component and added the image using the Xcode GUI and it did add the image but is on top of everything. I have tried several Stack Overflow answers (that's where I get the above code from), but I didn't have any luck.</p>
  
  <p>My folder structure looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/zmqMV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zmqMV.png" alt="enter image description here"></a></p>
  
  <p>My image is inside the Assets.xcassets folder.</p>
  
  <p><strong>UPDATE:</strong>
  I restarted Xcode and my code worked.</p>